What method is being called when I close an app - android

I read all about the activity lifecycle and it's methods.
I still couldt find an answer:
When I close my app, from the "open apps" menu (in galaxy4 it's a long press on the home button, in nexus5 it's the right button ...) what method is being called? if any?
In other words, my activity is launching a service.
I want to terminate the service if the app (activity) is being closed.
(onDestory is not reliable at all as said many times here before)
Thanks in advance!

onPause() is the only method that is called always. From onPause() state Android OS can kill this app for many reasons bypassing onStop and onDestroy. I don't think we can control this behavior of unexpected termination smoothly. Service can check for the state of the application periodically.

You will go through onPause() then onStop(). On pre-Honeycomb (API 11) devices, your app can be killed at any time after onPause() returns. No more methods called, period. Post-Honeycomb you will at least get onStop().

Locate with life cycle activity, one sugestion: http://developer.android.com/training/basics/activity-lifecycle/stopping.html
The system call the onPause(), after onStop() and last onDestroy(). You must analyze the best form for include your method. Pay attention in this moment.

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.

Is onPause() or onStop() called even if phone dies due to lack of power?

If I, for example, need to keep some very important data which the user can edit within my app, should I need to save them every time user changes such data or is it ok if I would save it within onPause(), onStop() or onDestroy() methods?
Can somehow application end without any of those methods calling? (For instance when battery runs out)
This certainly can't be done in onDestroy().
According to the documentation:
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, the application can end without calling any of the lifecycle methods.
In that scenario when the phone is shutting down, you can use the ACTION_SHUTDOWN Intent.
More info here. For all other cases onPause should do the work. Even in the link provided, there is a statement that onPause will be called if the app is in FG.
Apps will not normally need to handle this, since the foreground
activity will be paused as well.
However, if it is not so expensive operation, I would go with saving data after edit.
As per the documentation of Android activity life cycle, onPause is called when an activity is going into the background, but has not (yet) been killed.
Hence, in most Android documentation, and sample codes you will find onPause is used for saving any persistent state the activity is editing, to present a "edit in place" model to the user and making sure nothing is lost if there are not enough resources.
So in your use case, all you need to do is implement onPause for your Activity and write a code to Save the activity state (EditText content or any other ongoing user interactions). Android system will save the activity state which you can always get back in onCreate of your Activity when android launch your activity next time.
in this case please verify your phone activity via debug interface , some of phones are terminate your application this is force quit.

Android - what happens when the app is forcibly killed

Through Android's Activities doc, it is said that the methods onStop() and onDestroy() are not guaranteed to be called.
[...] once the activity is created, onPause() is the last method that's guaranteed to be called before the process can be killed—if the system must recover memory in an emergency, then onStop() and onDestroy() might not be called [...]
I would like to know, when this situation occurs, is the app also killed within the activities or just the activity itself is killed?
Answer is app process is also get killed and can be recreated.
https://developer.android.com/training/basics/activity-lifecycle/recreating.html
Please check http://www.vogella.com/tutorials/AndroidLifeCycle/article.html
Application with only stopped activities and without a service or executing receiver. Android keeps them in a least recent used (LRU) list and if requires terminates the one which was least used.

Activity's onDestroy is called instead of onResume when bringing the app to foreground from recent list

I have a singletop activity.
When i push it to background onPause() gets called.
When i bring the app back from recent apps, on Resume gets called.
Everything works fine.
but in API level 10 devices, (2.3.6)
when i bring the activity to foreground from recent list
onDestroy() is called.
onResume(),finish() nothing is called.
onDestroy() is called straightaway.
In what scenario could this be happening?
I thought device is running low on memory, but that is no the case.
It could be happening on any device as it does not contradict to Activity lifecycle. Therefore, you can not do anything to address that specifically. Instead, I would initialize everything properly in onCreate and start/stop whatever needed onResume/onPause.

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