I am trying to understand the actual concepts of the lifecycle methods of an Activity.
I am focusing on only primary lifecycle methods as mentioned below
onCreate()
onStart()
onResume()
onPause()
onStop()
onDestroy()
Can someone please explain a scenario where we can skip a method and rest lifecycle works perfectly fine?
E.g. if I swipe up and kill the application, onPause() and onStop() methods will be skipped and only the onDestroy() method will execute.
you cannot skip those methods, because when being implemented, they must call to their super class eg. AppCompatActivity - and when not being implemented, the will nevertheless be triggered within the super class. just see the documentation: Understand the Activity Lifecycle.
Can someone please explain a scenario where we can skip a method and rest lifecycle works perfectly fine.
It never can happen unless you manually call finish(), say, in onCreate() which is quite pointless. If a lifecycle method is "skipped" by the system (and the only scenario when it happens is an app process being killed) the rest of the methods are also "skipped".
If I swipe up and kill the application, onPause() and onStop() methods will be skipped and only onDestroy() method will execute.
Again, it never can happen by the reason described above.
onDestroy() method never calls when you swipte up and kill the application.
Related
I have a line of code that I should call after my activity is destroyed, so I override onDestroy() method and I call it inside. Like this:
#override
public void onDestroy(){
//my code here
super.onDestroy()
}
Now I noticed that the this line of code is not always executed when my activity finishes.
I read about this and some said don't depend on onDestroy() method to call something.
My question is I need to call the code from onDestroy() and I want it to always work. Any thoughts on this? And why onDestroy() is found in the first place if we cant depend on it to execute?
It is not a good idea to depend on onDestroy() for your cleanup works. It is better to perform these operation in onPause() or onStop(). These are the callback methods which the system calls whenever your activity goes out of view.
Now I noticed that the this line of code is not always executed when my activity finishes.
Either:
onDestroy() is called, or
You crashed with an unhandled exception, or
Your process was terminated (e.g., user revoked a runtime permission from Settings, Android needed RAM in a hurry)
I read about this and some said don't depend on onDestroy() method to call something.
Correct. onDestroy() is for cleanup of things assuming that your app is continuing to run normally. However, if your app is no longer running normally (you crashed) or your app is gone (process terminated), onDestroy() is not relevant.
I need to call the code from onDestroy() and I want it to always work. Any thoughts on this?
You will need to reconsider those two requirements, as they cannot both be true. Either initialize in onStart() and clean up in onStop(), or live with the fact that your cleanup code may not be called. Even the onStop() scenario is not completely guaranteed — in a multi-window environment, the user could revoke a runtime permission in Settings while your app is still visible — but we're getting into fairly unlikely scenarios.
There is no guarantee onDestroy it will ever be called, but i had rely on onPause to check if the Activity is finishing by calling if( isFinishing() ) // activity is about to finish yet this might not work properly.
I had rethink of my logic to not rely onDestroy at all.
boolean isFinishing ()
Check to see whether this activity is in the process of finishing,
either because you called finish() on it or someone else has requested
that it finished. This is often used in onPause() to determine whether
the activity is simply pausing or completely finishing.
https://developer.android.com/reference/android/app/Activity.html#isFinishing%28%29
These are the system calls for the method they will crash when they are called explicitly by the programmer...
Normally while calling finish(), we are expected to call onPause(), onStop() and onDestroy(). Most of the times it is working fine. But sometimes finish() only calls onPause() i.e. onStop() and onDestroy() are not calling sometimes. Why it is like so? What happens finish() for not calling onStop() and onDestroy(). I have put log in finish() method Activity.java. But in working and non working conditions logs are same. Please help.
According to developer docs, onStop() and onDestroy() are Killable:
Note the "Killable" column in the above table -- for those methods that are marked as being killable, after that method returns the process hosting the activity may killed by the system at any time without another line of its code being executed. Because of this, you should use the onPause() method to write any persistent data (such as user edits) to storage.
So onStop() and onDestroy() may or maynot be called, it depends on how system handle it.
When calling finish() on an activity, the method onDestroy() is executed this method can do things like:
Dismiss any dialogs the activity was managing.
Close any cursors the activity was managing.
Close any open search dialog
Also, onDestroy() isn't a destructor. It doesn't actually destroy the object. It's just a method that's called based on a certain state. So your instance is still alive and very well* after the superclass's onDestroy() runs and returns.Android keeps processes around in case the user wants to restart the app, this makes the startup phase faster. The process will not be doing anything and if memory needs to be reclaimed, the process will be killed
finish() Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().
finish()
I always think finish() will fire onDestory() immediately.But in 4.3 it seems not the truth.
I just want to know in which condition it'll happend ?
sorry,I can't put all my code on here.
It confused me a few hours,and I can't find any useful info about it.
From the docs:
onPause() is the last method that's guaranteed to be called before the process can be killed—if the system must recover memory in an emergency, then onStop() and onDestroy() might not be called. Therefore, you should use onPause() to write crucial persistent data (such as user edits) to storage.
It doesn't matter if you used finish() or the system killed your Activity on its own. If the system "wants" to recover memory, there's no guarantee that onDestroy() or onStop() will be called.
First, this answer assumes that you are referring to Android's Activity class and its finish() method and onDestroy() lifecycle method.
Second, it depends upon your definition of "sure":
Your process could be terminated in between finish() and onDestroy(), for reasons independent of whatever is triggering the call to finish()
A device manufacturer or ROM modder could introduce some screwy change that would break the connection between finish() and onDestroy()
The battery could go dead in between finish() and onDestroy()
Etc.
No, there is no guarantee onDestroy() will be called.
The same goes for onStop()
What you want instead is onPause()
This is true for all versions of Android, going back to 1.0 to the very latest. The Activity lifecycle can be initially very confusing for beginners. But it's not the beginner's fault, it's the fault of the framework designers.
Another mistake beginners make is to assume that onResume() is about resuming the Activity, when in fact it's the UI thread that is resuming while it is in its Activity. This is another point that the documentation goes over and over again, but really, it would have been a lot simpler if the android team had called that method something less ambiguous to begin with.
according to: http://developer.android.com/reference/android/app/Activity.html
Will allways be called,
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.
This is a text I have copied and pasted from this training tutorial.
"Because the system retains your Activity instance in system memory when it is stopped, it's possible that you don't need to implement the onStop() and onRestart() (or even onStart() methods at all. For most activities that are relatively simple, the activity will stop and restart just fine and you might only need to use onPause() to pause ongoing actions and disconnect from system resources."
I don't understand it. Because to the best of my knowledge, an activity is only stopped by calling onStop() and is only started by calling onStart(). How can an activity start at all without an onStart method.
Do you people understand what they mean in this paragraph?
I think they are confusing you with the word "stop" which appears to have multiple meanings in the paragraph.
I would rephrase it as
Because the system retains your Activity instance in system memory
when it is not in the foreground, it's possible that you don't need
to implement the onStop() and onRestart() (or even onStart() methods
at all. For most activities that are relatively simple, the activity
will suspend and restart just fine and you might only need to use
onPause() to pause ongoing actions and disconnect from system
resources.
The point being is that the App can appear to be stopped, when in actual fact, the system has simply paused it and hidden it from the screen. When the user launches it again, the App doesn't need to start (because it technically hasn't stopped), so it is simply resumed.
When you make an Activity and extend the base class Activity, there is already code in the onStop(), onStart(), and onRestart() methods in the base class.
Your activity simply extends these methods, meaning that you could add more code to them by Overriding them.
So, even though Activities are only started and stopped through those methods, you do not have to explicitly override them in your application. In most cases you won't even have to worry about them: They will be called by the base class from which you are extending.
Please make sure, An Activity starts from onCreate method , then onStart is called by system. If you override onStart method then your overridden method will be also called after onCreate method. If you don't override , then default version of onStart is called.
onStop is called after onPause.
Please check this link , and take a look at Activity life cycle . Your concept will be clear.
Difference between onCreate() and onStart()?
you can use an Activity just fine without - if you need to do something special in onPause() you can override the method:
#Override
public void onPause(){
super.onPause();
// Your magic here!
}
Same goes for onStart(), onStop() etc. You don't need to override the methods but you can if you need to do something specific.
I would like to call onDestroy() at the end of my Activity.
Here is what i put :
#Override
protected void onDestroy()
{
unbindDrawables(findViewById(R.id.rootView));
super.onDestroy();
System.gc();
}
But, it is never called. Indeed, when I use auto completion, onDestroy never appear...
but onPause, onCreate, onStop .... yes
So do I miss something here ? I might be so stupid after all...
You should call finish().
onDestroy is called automatically, althought you should not rely on being called.
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.
onDestroy may be not called
Note: do not count on this method being called as a place for saving
data! For example, if an activity is editing data in a content
provider, those edits should be committed in either onPause() or
onSaveInstanceState(Bundle), not here. This method is usually
implemented to free resources like threads that are associated with an
activity, so that a destroyed activity does not leave such things
around while the rest of its application is still running. There are
situations where the system will simply kill the activity's hosting
process without calling this method (or any others) in it, so it
should not be used to do things that are intended to remain around
after the process goes away.
Derived classes must call through to the super class's implementation
of this method. If they do not, an exception will be thrown.
Thanks everybody for your help.
I have now understand that onDestroy may not be called.
I don't know why, but now Eclipse prompt me the onDestroy method.
So it is called but not where i want to.