Question

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

Was it helpful?

Solution

I believe you are rather confused to ask this question.

In order to get a good comprehension of what is happening, you should take a look at the lifecycle graphs that can be found 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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top