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.
Related
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.
I am porting an iOS App to Android, and am having a little difficulty with the terminally.
I want the App to sync when starting/coming to the foreground (i.e. the user selects the App) and when the App goes to the background (e.g. when going to the main screen and start using another App.)
Is there an easy way to do that in Android?
I added onResume() and onPause() to my Activities, but this will be triggered when the activity resumes or pauses, not when the App as a whole pauses (and results in a lot of sync each time the user does something).
Update:
The Sync operation is a Service which is called when the App needs to get new/updated information from the server. As such, this needs to be done when the Application is started/resumed by the suer (after inactivity for some time) and it needs to do this when the user actively stops using the Application.
when an activity goes to background,
onPause() -> onStop()
methods will be executed in sequence, if activity is partially visible, only onPause(), if activity is completely invisible by another activity, onStop() method will be executed after onPause().
onDestroy()
method will be executed if you either called finish() or when there is no enough memory in the system and system destroys it. You can't rely onDestroy() method unless you call finish() method.
It's best to use onStop() method to save your data.
nr4bt has a valid point and depending on your required functionality, a good option.
A global approach would be to start a Service when your application starts.
Make this Service responsable for your syncing operations with suitable callbacks to your active Activity.
Make this Service aware of your alive/paused/stopped Activity's. When the last one is finished -> Do your final sync and kill the Service
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()
When I stop a service using the stop button under the Running Services tab, the method onDestroy() is called.
But when I force stop the application, onDestroy() is never called.
Any explainations about this?
or maybe a solution to fire onDestroy() when force-stopped?
When your force stop an app, exactly that happens - It is Force Stopped. No warning, no callbacks, just stopped. The entire process is killed, and none of the running components (Activities, Services etc) are given any warning.
There is absolutely no guarantee that onDestroy() will be called. Move any application critical code into onPause(), which is called under most circumstances.
From the documentation:
Once the activity is created, onPause() is the last method that's guaranteed to be called before the process can be killed... onStop() and onDestroy() might not be called. Therefore, you should use onPause() to write crucial persistent data (such as user edits) to storage.
To reiterate this point, Force Stop isn't intended to be graceful and exit the app in a caring manner. If you have critical code that must be run each time app finishes you need to run it in onPause().
When the application gets force stop, Process.killProcess() is called but not onDestroy() function. Go through this link. You will get some idea.
Android force Stop callback to application?
I am assuming you have code that you want to execute in onDestroy() referring to your line:
"or maybe a solution to fire onDestroy() when force-stopped?"
The Service method public void onTaskRemoved(Intent rootIntent) is what you are looking for, it will be called when the app is force-stopped.
I know it's an old question, but I was having the same issue and in my case I was using a binding service, so even after called stopSelf() Android does not call onDestroy() method, in order to force it I need to call unbindService() first
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.