What elements need to dispose when i'm closing an activity?
I'm using lists,sqlite,recyclerview,listview and so on. If i will call finish inside an activity, doesnt dispose all my activity elements-objects even they are not null?
finish();
finishAffinity();
Certain elements in Android will continue to exist after your Activity has finished, such as BroadcastReceivers.
These elements need to be unregistered when your Activity finishes to avoid things like memory leaks or other weird behavior.
There's no definitive list for this, because it's assumed you will read the documentation of each respective class or API you use and know whether or not it needs to be unregistered/destroyed along with your Activity.
Generally, java's garbage collector will take care of disposing any objects that are not required anymore, however, as #TheWanderer already said, some objects need to be deregistered. Again, please read the docs of the classes you're using as they will indicate if they need some special treatment.
(Hint: Place the cursor in the class name that you want the documentation for and press Ctrl+Q in Android studio to view the documentation)
Related
I have quite a bit of experience in Java, and up until I noticed this bizarre behaviour in my first android app I thought any references I create will always point to the same Object unless some code with access to the reference changes it.
I had an activity with to two fragments under it inside a ViewPager. In the activity I maintain references to the two fragments (private fields) to interact with them. On orientation change my fragments are stopped (and apparently destroyed as well) so I am creating them again from the activity because they have complex state that I don't wanna bother "parceling" or "serializing" into a bundle... This may not be best-practice, but hat's not my question though. It's more about how the Android JVM and libraries manage to automatically change the activity's private reference to the fragment (which I'm setting in the onCreate method) to point to an automatically constructed fragment using the default constructor. I understand that android can destroy fragments and construct new ones later and invoke onSavedInstanceState or whatever to allow the developer to recover their fragment's state; but how does it also find and change the existing reference in the activity to point to the newly constructed fragment.
This seems to be a JVM-level feature (more than just simple reflection), that allows Android to figure out all existing references to the fragment (or is it? what am I missing?) in order to modify them when a fragment is re-created. Whatever the case, is this and Android-specific JVM feature? Does a similar thing exist in other Java implementations such as Oracle Java or OpenJDK?
Please keep your answer about the weird automatic reference juggling part, and not about how I'm managing the activity/fragment life-cycles (I'm aware I could do better, and I'd love to read your suggestions, but keep those in the comments or the second part of your answer). I've done a lot of debugging and that's what I observed: the fragment reference was indeed changing, and I managed to fix it by manually resetting the reference to the reference I explicitly set in the onCreate method - a seemingly redundant operation.
I found a couple of places that suggest System.exit(0), but then I read a couple of places that this is bad practice, since android handles closing an application for you.
My issue is, I have an auto logout function that takes users back to the login page, BUT I'm wondering what's in memory at this time. Do I have to have a de-constructor setup to set my values to null when activities change or do objects automatically get nulled out between activity changes?
You can use Activity#finish() when you're done with any Activities. For example, when the auto-logout occurs, call finish() on any open Activities.
https://developer.android.com/reference/android/app/Activity.html#finish()
I'm confused regarding with some basic android development concepts, my question is not pointing at a particular code, thats why I dont include any.
Let's say that I have an activity inside of which I have a container in which I load a couple fragments (they are multiple instances of the same fragment), now the activity is populated, and inside one fragment I press a button that opens a new activity, it doesn't matter what may happen in that activity, the thing is that when I press a button it should take me back to the previos activity, I know that pressing the back button or using .finish(); will take me back to my already-populated activity, but I want to know, if that is the correct thing to do, or should I finish the activity as soon as i leave to the next one and when I want to go back create a new instance and repopulate it, if so, where should i store the variables?
Let's say that the fragments that I mentioned are "alarms" for an alarm application, and when I create it I call AlarmFragment newAlarm = new AlarmFragment(); and then I add that alarm to an arrayList in my alarms activity (java class) getListOfAlarms().add(getAlarmsAmount(), frag); which remains on the activity that has the fragment container, the question is, are these variables created in the right place? Because I am leaving them inside the activity itself right? What could happen if the activity is destroyed? I've been told that I should create an SQL database for storing those variables. I am not talking about long term saving but variables that I will be using at runtime
Can someone explain me these concepts a little bit? A link to a place where it is explained will be great too.
Your question seems like it has many parts.
In Part 1, this is what I think you are talking about:
1) how you decide to allow the user to get back to the first activity is really up to you. And 2) what you leave in the Back Stack is also up to you and what you want to define for the users' capabilities within your app. For example, if you want them to only be able to use a button that you define inside Activity 2 container, that is fine. However, you are not required to provide a button in Activity 2, and you are certainly allowed to use the Up Action in the App Bar for navigation. If I were you, I would read more about the Tasks and the Back Stack http://developer.android.com/guide/components/tasks-and-back-stack.html
You also mention this idea of having to "finish an Activity" with .finish(). I don't think that is usually necessary, but it is available to you if you want to use it based on what you decide for your app's logic (and what the user should and shouldn't be able to do).
With the Back button, Activity 1 will appear as if just initialized when you get back from Activity 2. Try it out. Also run some Log statements based on the simple diagram I provided and the Lifecycle "callbacks" (put these methods in your Activities and throw a Log statement in each to get a better sense of where you are in the Lifecycle) http://developer.android.com/guide/components/activities.html#Lifecycle
As for Part 2 of your question, I would try/set-up some of the above first, then start to experiment with a single variable to see what happens to it between Activities. There are a lot of "what ifs" to your question. You don't necessarily have to create a DB to store your variables, but that could certainly be an option. Take a look at the Developer Guide for most of the Data Storage options: http://developer.android.com/guide/topics/data/data-storage.html
If you are concerned about losing data when the Activity is destroyed, I might consider creating a database. Read the following for more info on recreating an Activity when you have gone elsewhere and are returning: http://developer.android.com/training/basics/activity-lifecycle/recreating.html In particular Saving Your Activity State: http://developer.android.com/training/basics/activity-lifecycle/recreating.html#SaveState
There's also an SO post on this: Saving Android Activity state using Save Instance State
When a single activity crashes, all the activities in flow crashes.
As all activities crash, each generates its own error dialog, so can we have a single dialog for all crashes?
Try catch is already implemented.
You use a singleton static final object to handle all your diablog boxes across all your activities. One such singleton object is the application object, see http://developer.android.com/reference/android/app/Application.html, but you don't have to use that one if you don't want to.
Also, you have to make sure that try/catch is implemented absolutely everywhere, wherever the android operating system makes calls into your app code. I'll find that I've got try/catch in most places in my code, but somehow when I'm trying to develop quickly I accidentally miss a few places and of course they're the ones that end up causing me a problem!
I'm a little confused by the difference between Java and Android Java. Let's say I have an Activity class AndroidX. There is no main function and there is no AndroidX() constructor as we know it. I realize that onCreate() most probably initializes the AndroidX Activity, but why is there no main? What's the difference?
Consider that your activities are many *main*s and your manifest directs the execution to one of them.
Also consider that the constructor as we know it before is hidden and now it is always called onCreate()
Fair enough to keep going?
This graphic may help some.
http://developer.android.com/images/activity_lifecycle.png
In the Activity documentation they elaborate on what each function is meant for (i.e. onCreate(), onResume(), etc).
http://developer.android.com/reference/android/app/Activity.html
There is no "main" because that assumes that your app is either running or not running. But on android there are many other possible states your app could be in paused, stopped, started, etc...
Check out this link for an excellent overview of the Android Activity lifecycle.
How onCreate works is described in the Activity page of the Android Developer Reference. Specifically here:
onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.
In a sense you can consider this method the constructor for your Activity, as the initialization is handled there (see the Activity Lifecycle).
As for main, consider it hidden from you. Generally what you do is register listeners for UI elements such as buttons or text fields, then act on input from those UI elements. These listeners handle calls to your methods which might manipulate data or change how the UI displays.