Android and shut down application - android

I just want to ask a small question. How long my application will be stored in background before Android kill it automatically. And, will be onDestroy() event fired when it happens?
Thank you!

As explained in the Activity Lifecycle onDestroy() is called when "The activity is finishing or being destroyed by the system" That means that you either manually call finish() in your code, or the phone's system needs the resources occupied by your app. When this happens(for example when another app needs those resources) the OS will call onDestroy() on your app.

onDestroy() isn't necessarly called especeally when your app is getting killed.

Related

Send App back to Recent app stack android

I am working on a WebRtc Calling App. When my application is in background when incoming call come the Calling Activity PopsUp for call . And When call hung Up i need to send app to the recent background state.
So i came around a solution moveTaskToBack(), which is working perfectly but the problem is after calling Activity finish it shows the Last Activity for 2 or 3 seconds and then going back to recent app. Also tried in onStop() but the result remain the same .
#Override
protected void onDestroy() {
moveTaskToBack(true);
}
Is there a better Alternative of moveTaskToBack() without any Delay? If anyone need more clarification on question let me know in comments. Thanks in advance.
You shouldn't do it onDestroy, because it's not guaranteed that to be called in Activity Lifecycle.
In the onStop() method, the app should release almost all resources that aren't needed while the user is not using it. For example, if you registered a BroadcastReceiver in onStart() to listen for changes that might affect your UI, you can unregister the broadcast receiver in onStop(), as the user can no longer see the UI. It is also important that you use onStop() to release resources that might leak memory, because it is possible for the system to kill the process hosting your activity without calling the activity's final onDestroy() callback.
Problem might be the time takes to calling onDestroy. You can try to call it where you are finishing activity.

How do i know when my android application instance no longer exists?

I am making an application for which I want to logout the user whenever he removes the app from recent used apps or the app no longer it exists in the background. How can I do that?
You should probably put your logout code in your onDestroy() method of all the activities you are using in your app.
Actually, to be safe, you should save things in onPause() because (at least pre-3.0) your process can be killed by the OS after it calls onPause(). This implies that onStop() and onDestroy() may or may not be called , but then it would make no sense to put your logout code in onPause() method.
In any case, there is no guarantee that onDestroy() will ever be called. If the OS decides to kill your process it won't bother calling onDestroy() on any activities.
But if there is any place you should put your logout code it should be onDestroy().

activity manager clear all recent and kill process in background?

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

Will activity run onDestroy when system kill it?

I am wondering that will activity run the method "onDestroy" when killed by system?
for example, when the state of "activity A" is onStop ( user may press the Home button directly ),
at the same time, system find out that the memory is not enough so system have to kill some background processes to keep foreground activity alife, say system kill activity A.
Will activity A run the method "onDestroy" in this situation?
It will purely depend on the system condition at that time. Docs clearly says about onDestroy() that:
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.
See Here
From the developer.android.com :
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. In extreme cases, the system might simply kill your app process without calling the activity's final onDestroy() callback, so it's important you use onStop() to release resources that might leak memory.
So, android usually will call onDestroy() of your activity before it is killed but it is not guaranteed.
Link : http://developer.android.com/training/basics/activity-lifecycle/stopping.html
Depends, as when system kills an application, it's associated PID killed by it directly. As Android is nothing but Linux, it sends SIG9 (9 number signal is "kill")/ kill (Application's PID) to kill application without invoking it's callback methods.

onDestroy being called

I can't figure out the onDestroy() behaviour.
My question is: Is there any chance that an activity will be killed without calling it's onDestroy() while not killing the hole app?
I mean, Could it be that I'll get back to my app (to an activity other then the activity that the launcher calls) and be in a situation where one activity was killed without calling it's onDestroy()?
I have a need to know that if I get back from the background to an activity that there is no way some of my activities where killed without it's onDestroy.
Thanks!
No i don't think so , when your application get killed because of lack of Memory your whole app process would be killed so in this situation onDestroy() may not be called and your app will back again on your launcher Activity unless you can save your application state on onPause() state before your app get killed.
yes, Android will kill a least frequently used activity if there is not enough memory is available for the newly started app. Also the back button triggers the onDestroy(). A best bet is to save your app state. Here is an example to a similar question how to save and restore your current instance.
As stated in the API documentation Activity#onDestroy():
Note: do not count on this method being called as a place for saving
data!
http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29
And don't forget to call super.onDestroy()

Categories

Resources