Reputation: 65
Here after on Destroy I print a Log statement , will the Log get shown as the app already gets destroyed and no Instruction can be executed after app gets destroyed .
@Override
protected void onDestroy() {
super.onDestroy();
Log.v(TestActivity,"App is currntly getting destroyed")
}
So will "App is currently getting destroyed" be printed? If it won't be printed then how can I execute code in onDestroy Method?
Upvotes: 1
Views: 1363
Reputation: 10605
The answer to your question is, "It Depends".
The reference documentation says that onDestroy
is not guaranteed to be 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.
So, yes, you message will be called unless Android destroys the process first.
Documentation also say that you must call through to the super classes implementation...
If you override this method you must call through to the superclass implementation.
...but it doesn't say what order the call should be made. Because of that, I would look at Activiy source to see what it is doing. Here is the (version de jour) source code for onDestroy
from Google Source.
protected void onDestroy() {
mCalled = true;
// dismiss any dialogs we are managing.
if (mManagedDialogs != null) {
final int numDialogs = mManagedDialogs.size();
for (int i = 0; i < numDialogs; i++) {
final ManagedDialog md = mManagedDialogs.valueAt(i);
if (md.mDialog.isShowing()) {
md.mDialog.dismiss();
}
}
mManagedDialogs = null;
}
// close any cursors we are managing.
synchronized (mManagedCursors) {
int numCursors = mManagedCursors.size();
for (int i = 0; i < numCursors; i++) {
ManagedCursor c = mManagedCursors.get(i);
if (c != null) {
c.mCursor.close();
}
}
mManagedCursors.clear();
}
// Close any open search dialog
if (mSearchManager != null) {
mSearchManager.stopSearch();
}
}
Though this could change at any time, we can see that onDestroy
in the super class will clean up resources (as documentation says). So, does your implementation of onDestroy
rely upon any of those resources? That would dictate that your code should be called before super.onDestroy()
. If not, then the order doesn't matter (except that your teacher has told you what order to use).
Upvotes: 3
Reputation: 4678
It is not guarantee that onDestroy() will always call. You can do your cleaning stuff in onStop() method.
Upvotes: 0
Reputation: 13617
Call the super.onDestory()
after your code executed.
@Override
protected void onDestroy() {
Log.v(MainActivity,"App is currntly getting destroyed")
super.onDestroy();
}
Also it won't be called if the app destroyed by the android for memory allocation.
Its better to use onStop()
method to save your data.
Upvotes: 0