I have tried to research exactly when the onDestroy method is called for an activity, but I've read some confusing and conflicting information. In general, my question is: under what circumstances is the onDestroy method actually called on an activity? More specifically, if I have two activities, activity A and activity B, if activity A is running and I create an intent and switch to activity B, is activity A only stopped, or is it destroyed?
Like stated in the official documentation:
onDestroy()
The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.
In your example the Activity A is stopped and could be destroyed by the System
Note per documentation link above:
…do not count on [onDestroy()] being called as a place for saving data … [see] either onPause() or onSaveInstanceState(Bundle).
onDestroy() is called whenever:
The user takes out the activity from the "recent apps" screen.
The user takes out the activity from the "recent apps" screen.
onStop() is called whenever:
The user leaves the current activity.
So in your example, when the user launches Activity B, Activity A called onStop().
EDIT:
The onDestroy() method is not always being called, according to the documentation. onStop() is always called beginning with Honeycomb, so move code you explicitly need to do before the activity stops to there.
Starting with Honeycomb, an application is not in the killable state until its onStop() has returned.
https://developer.android.com/reference/android/app/Activity#ActivityLifecycle
Hope this helped :D
Related
I want to use Application.ActivityLifecycleCallbacks to monitor how many activities are there in the back-stack. Can I increment/decrement counter in onCreate/onDestroy to handle this?
onDestroy is NOT guaranteed to be called every time an activity is destroyed.
If the user clicks back to destroy it, onDestroy will be called.
If the user swipes the application from the recent app menu, onDestroy will NOT be called.
If the application crashes, it's undetermined if it'll be called (from my experience, it isn't called).
Is onDestroy always called when android destroys activity to save memory?
Yes
Documentation:
The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.
I want to use Application.ActivityLifecycleCallbacks to monitor how
many activities are there in the back-stack. Can I increment/decrement
counter in onCreate/onDestroy to handle this?
Better to counter in the onStart() and onStop() methods, onCreate() doesn't guarantee visibility. For example if somehow something stopped onStart() from happening.
onDestroy() is the final method that is called on an Activity instance before it’s destroyed and completely removed from memory. In extreme situations Android may kill the application process that is hosting the Activity, which will result in OnDestroy not being invoked. Apparently most of the Activities will not implement this method because most clean up and shut down has been done in the OnPause and OnStop methods.
For more details please visit Android Developers Portal.
(https://developer.android.com/reference/android/app/Activity.html "Android Developers")
I have noticed that the onDestroy() method of a fragment gets called multiple times - why would this be? I would expect only 1 call.
onDestroy() = The final call you receive before your activity is destroyed.
This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space.
You can distinguish between these two scenarios with the isFinishing() method.
It's normal for an Activity or Fragment to get onDestroy()-ed multiple times. For example, when you change the device orientation, the current Activity goes through onDestroy() and then a new instance of the same Activity goes through onCreate(), now in the new orientation.
You might be confusing this for finish(), which gets called when the Activity gets "killed" per se and happens only once when you navigate away from it.
This question already has answers here:
Android activity life cycle - what are all these methods for?
(9 answers)
Closed 9 years ago.
which method is called in current activity when moving to another activity? Let's take i am in Activity A, from A i move to Activity B.
I want to execute a particular piece of code while moving to Activity B
Nothing is called on your current activity specifically for "moving to another activity".
Your current activity will be called with onPause() when it leaves the foreground from an input standpoint, and it will be called with onStop() when it is no longer visible. However, those will be called for any trigger for those events, including the user pressing HOME or BACK.
You can write your code inside Activity B's onCreate().
onPause() method is calledwhen it leaves the foreground and onStop() method is called when the activity is no longer visible.. Also refer to the complete activity life cycle in the below mentioned link
Refer to table 1 of the activity life cycle in this link Activity Life cycle
Activity life cycle
onPause() and then onStop() methods will call when you change the activity ,,if you finish the current activity while moving to the second activity the onDestory() also called..
Here is an intrusting issue that if you start an your new activity in onStop method the onPause and onStop method wont call again :)
onPause and probably onStop (if the new activity takes up the entire screen) will be called on A. onDestroy 'can' be called at this point, but the system will keep it alive if it can.
onCreate, onStart, onResume will be called on B, unless B is already in the stack and you either use some flags in the intent, or B has a special launchMode, in which case, rather than onCreate, onNewIntent will be called.
I have an activity that starts another then if the user cancels from the second I call finish() and return to the first.
Question: Is there a method that gets called when I get returned to the first activity?
Thanks for any help
Yes, onResume() is called.
Check the :Activity Lifecycle
See the lifecycle:
Yes there is onStop() and onStart(), for more details see:
http://developer.android.com/reference/android/app/Activity.html
The original application will receive whatever callbacks are on the activity life cycle flowchart that are between the application's current state and "Activity Shut Down" state. onDestroy() is the only one guaranteed to happen.
If the finished application was on the top of the stack then the Activity that replaces it on the top of the stack will receive at least onResume(). If it was completely invisible (i.e. it had received onStop(), then it will also receive onRestart() and onStart() before onResume().
If the finished Application was not on top of the stack then no other Application is notified.
Here is the scenario:
I have two Activities. Lets name them Activity A and Activity B.
Say Activity A is open. Now, when I go and open Activity B, Activity A is closed because the onStop() method is called.
Now, when I flip back to Activity A, the onCreate() method is called, but I want the onRestart() method called instead. How do I do this?
You cannot influence the livecycle of your app like that. There should be no reason to rely on onRestart(). If you use onStart() it will always be called no matter if the Android OS killed the app process in the background.
Check out this doc for further information:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Damn beat me to it but here goes anyway
According to the Activity Lifecycle onCreate() is called again if the Activity was removed from memory because the OS deemed that another app needed the memory. In this case, you can't ensure that onRestart() will always be called for your Activity.
Like already stated you must find a different way of achieving your goal by using the other Lifecycle methods such as onStart() or onResume
I'm not sure if it fits your needs, I had to do an update service that starts the first time I open ActivityA (main Activity) and stops when exiting from ActivityA (not returning back from ActivityB),
I've placed the "start code" in onCreate() when savedInstanceState is null and the "stop code" in onDestroy() if isFinishing() is true