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.
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...
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.
In my code i call finish on a activity but the onDestroy method of this activity not be called. would anyone can tell me why? when the onDestroy method will be called?
onDestroy() is called only when system is low on resources(memory, cpu time and so on) and makes a decision to kill your activity/application or when somebody calls finish() on your activity.
So, to test your code() you can make a test button, that will call finish() on your activity.
I believe you don't need to call all this stuff in onDestroy() until adap is not a critical resource. And even in that case android system has mechanisms to properly dispose them.
For More Details you can Refer Here
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.
I understand when an activity closes, onDestroy() is called. But this is not done always right? Sometimes, onPause() is called.
So suppose I want to clear some memory when an activity closes, where exactly do I do it? Since onDestory may not be called, I cannot keep it there either right?
Elaborating:
I have 2 activities A1 and A2. A1 is hsown in the startup of the app. A1 calls A2 later. Suppose I create a class object in onCreate() of Activity A1. This object must be deleted when the I exit the app, i.e when the app is no longer visible. Is the best place to do this onDestroy() or onStop() of A1?
I guess onPause() may not be the right place, because onPause() will be called when A1 calls A2 and I dont want to delete the object then.
-Kiki
I think you mean onPause(), there is no onSuspend() method. If your activity closes cleanly, it will call onStop() and onDestroy().
If the system is running low on memory and wants to kill your activity, then onPause() is guaranteed to be called before your process is killed, but that's the only guarantee. The methods onStop() and onDestroy() may not be called. So you should cleanup in onPause().
However... don't forget that your activity can transition many times between onResume() and onPause(), so you don't want to do too much allocations and cleanups in those two methods, they should be quick.
You have to decide how best to cleanup in onPause(), and what you actually have to do, if your activity needs to do something before it is killed off during low memory situations.
Google's docs say to do things that need to be done before exit in onPause(), because yes, in low memory situations onDestroy() may not be called.
I don't think that onSuspend() exists. It is not mentioned inside the documentation. Maybe you are referring to onStop().
When onStop() is called, the activity is no more visible but not yet destroyed. The termination really takes places after the onDestroy() call, whether it is terminated by a call to finish() or memory requirements from Android as the documentation says. Then, the right place to free memory in my opinion is onDestroy().
If you have to stop CPU-intensive operations that are related to user interaction, do it in onPause().