My application launches 3 activities.
When I want to exit, I close these 3 activities calling finish method. The activity onDestroy methods are then called but the process is still alive...
How is it possible ?
This is perfectly normal. Android will keep your process around until such time as it needs to reclaim that process' memory. That way, if the user immediately returns to your application, your application will appear quicker -- Android does not have to fork a process and load your application into memory.
Try:
System.exit(0);
It will kill your Activity-process.
Edit:
As mentioned in the comments below. This works much better:
android.os.Process.killProcess(android.os.Process.myPid());
Related
I want to understand and simulate when does Android call onDestroy() of my activity, without destroying the entire process. I'm not calling finish(), and I want to make Android destroy my activity on it's own.
From the activity-lifecycle documentation:
The system never kills an activity directly to free up memory. Instead, it kills the process in which the activity runs, destroying not only the activity but everything else running in the process, as well.
But the Android Activity documentation says:
This (onDestroy) can happen because [...] the system is temporarily destroying this instance of the activity to save space.
So which one is it? Does Android destroy activities when it's low on memory, or does it only kill entire processes?
I want to simulate a situation where Android kills the activity without killing the entire process.
I can mimic this by using the "Don't keep activities" developer-mode setting, but I want to understand how can this happen in the real-world.
I want to make Android destroy my activity on it's own.
Android does not do that, other than through your actions (e.g., finish()) or user actions (e.g., BACK navigation, configuration changes).
So which one is it?
The former (Android kills processes, not activities). See this answer from the woman who wrote this stuff. FWIW, also see this decade-old blog post of mine.
As we know Android would kill a paused Activity in some conditions. And there is a FIFO back stack of Activities.
My questions are as follows:
How Android kills an Activity without pulling it from the back stack (it might affect the top active Activity)
After killing it, what things are released from this Activity? And can I still get this Activity's instance?
Android does not kill Activities "separately", it kills the whole app process with all Activities.
The only way to get an Activity killed by the system is to set Don't keep Activities flag in device's Developer Options. However this option is just for development, not for applications in release.
Activity can't be killed but Os can kill the whole application. In this case you can try finish()/finishActivity()/context.finish() to finish the activity. While you finish an activity backpress will not return to the previous activity.
System can't kill activity, it can call the whole app.
And when it kills the whole app, it doesn't call any method from activity (onStop(), onDestroy(), etc)
Finally, you can't get activity instance.
activity manager clear all recent and kill process in background?
-Can system destroy only one or some of my activities to recover memory?
-Will system kill the whole process of my application? Will all activities be nicely destroyed?
i'll use code but don't work
mgr.killBackgroundProcesses(p.processName);
When your activity receives a call to the onStop() method, it's no longer visible and should release almost all resources that aren't needed while the user is not using it. Once your activity is stopped, the system might destroy the instance if it needs to recover system memory.
- http://developer.android.com/training/basics/activity-lifecycle/stopping.html
So yes the system can destroy your activity when its not visible.
Depending on how its destroyed, it might save an instance by calling the onSaveInstanceState(), and when it resumes it will call the onRestoreInstanceState(). This article has more information on the questions.
First of all I have to say that Google does not recommended to kill any process.
This migh be helpful for you: Is quitting an application frowned upon? and
Android destroying activities, killing processes
I hope these links will answer all your questions.
If you want to finish your activity use finish();
Here are the examples how you can destroy your app: https://stackoverflow.com/a/26586015/3864698
Well apparently my android application doesnt close when I finish the activity so it there a way to force close it including any activity in my app that could be opened?
The fact that the process is still alive does not mean that the Activities did not finish. The process might be kept for a while until the OS decides to kill it.
If you have activities that do not finish properly, make sure that you did not leave a thread running.
There is no method in the API to close an Application. It is up to the OS to terminate it when it is convenient. The last resource is to kill the process, but you should never need to use that in your apps.
Make sure that threads that you have started terminate, and then invoke finish
Btw. How have you verified that you activity isn't closing?
Call finish() at the point when you want it to close. For example in onPause() if you want it to finish when the activity is no longer the topmost one.
I personally suggest you simply make sure your app does not do anything when it is not active and let the system worry about 'finishing' it.
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
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:
Activity lifecycle
Background service lifecycle
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.
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.