views:

77

answers:

2

Hi,

I know my question caption must have sounded really vague. But let me clear it out here.

Say I have an android application over a middleware stack. In onCreate() of my activity, I initialise my middleware modules.

In its onDestroy(), I must de-initialise the middleware. Now my middleware calls may take quite some time to process. So I want to know how much time the onDestroy() function has, and see whether my deinitialisation can take place within that time.

Is it reasonable to keep my de-init in the onDestroy()?

Also, suppose I initialise the middleware in onCreate() of activity A1. On a button click, activity A1 switches to activity A2. In low memory situations, the LMK will kill the activity that has not been in use for some time. In such a case, won't activity A1 be killed? When activity A1 is killed, will all instances I create in A1 also get destoryed?

Regards, kiki

+3  A: 

I believe you are rather confused to ask this question.

Look at the lifecycles graphs on developer.android.com

You will see that Activity.onDestroy() only gets called in the case of a controlled shutdown of the activity - something that happens extremely rarely, as the Android OS can kill your process in a variety of states without ever calling your onDestroy() method.

What and why do you need to de-initialize?

  • If you're worried about releasing resources, then most of them will get released anyway when/if your process is killed.
  • If you are worried about saving the user's data (your application's state) then you should override onSaveInstanceState() and onRestoreInstanceState()

If you really want an answer to your question, then here it is:

  • While it is running onDestroy(), your app has (probably) as much time as it would like to - the fact that it is even running onDestroy() means that the OS did not select it to be killed. But it will most likely not matter: for one, onDestroy will never be run in most apps, and if the OS changes its mind and decides that your app must die, it will kill it even if it is running onDestroy.
jhominal
@jhominal: A small note regarding when to save the user's data. One should be aware that `onSaveInstanceState()` only will be called if the application is being killed by the system. It will _not_ be triggered if the application exits due to the user either pressing the back button, or `finish()` being called in your code. So if you want to be 100% sure that you save the current application state, this should be done in `onPause()`.
Nailuj
And another note: when you say `your app has (probably) as much time as it would like to`, this is not correct. If your app had as much time as it would like, it could easily freeze up the whole system. So the 5-second rule still goes.
Nailuj
@Nailuj: The app runs in its own Linux process. If it begins to consume too much resources in the `onDestroy` method, (the user has already left the app), the process will be unceremoniously killed by the system - but the condition for that to occur should be tied to a quantity of consumed resources rather than a specific duration.
jhominal
Another note: The "5 second rule" is in no way universal. It simply says: "If your app takes more than 5 seconds to react to user input, then I (Android) will show the user a dialog to kill you." Nothing more, nothing less: your app could consume no resources at all and still suffer that rule (for example by calling `sleep` on the UI thread). But in a call to `onDestroy`, your app has not been responsible for user input for some time, so it does not apply.
jhominal
@jhominal: ok, I can see that it might not be a strict 5-second rule for performing your work, but to say that you have as much time as you'd like is probably a bit off as well - since you have no guarantee that the system won't kill the process if it is in need of resources.
Nailuj
@Nailuj: I could have phrased it better by saying that there was no time limit for how long `onDestroy` could run. Added a bit at the end to clear misunderstandings.
jhominal
@jhominal: +1, very nice and clear now :-)
Nailuj
+3  A: 

http://developer.android.com/guide/practices/design/responsiveness.html:

In Android, the system guards against applications that are insufficiently responsive for a period of time by displaying a dialog to the user, called the Application Not Responding (ANR) dialog

The ANR dialog will normally pop up if your application is un-responsive for 5 seconds. As pointed out by jhominal, the onDestroy() method is probably not where you want to do your clean-up/save preferences/etc.

Regardless of where you choose to do this, be it onDestroy(), onSaveInstanceState() or in onPause(), I believe the general 5 second rule will apply. If what you're doing takes more than 5 seconds, the ANR dialog will show and the user can choose to force-close your app.

Edit: If your application is in the background, it might be (probably?) that it is killed directly without the ANR dialog being displayed if you violate the 5 second rule. But I do not know this for sure, only assuming.

Nailuj