I need some help in application exit handling. My application has multiple Activity say : - EntryAct, Act1, Act2 .... Act10.
When user presses home key on any of these Activities, I need to do some flag setting.
Handling home key in every application Activity is not possible! Can someone tell how this can be achieved?
Can't change anything in OnPause,OnStop or OnDestory of all activities Act1... Act10. :-(
In android, by pressing a back button or home button. So put an event key listener for back & home button and terminate the service.
The "onActivityDestroyed" will get called when the app is closed, so if you can check if the app is in background when it is called (so the app is already closed) you can grep exactly the moment when the app is being closed.
there's no way to determine when a process is killed. From How to detect if android app is force stopped or uninstalled? When a user or the system force stops your application, the entire process is simply killed. There is no callback made to inform you that this has happened.
your issue can be solved if you use a custom Application class. say like
public class MyApp extends android.app.Application
{
}
and inside this put your code that you want to be called anywhere in your app.
Now only you require to get the application object in your Activity
like this
MyApp app = (MyApp) getApplication();
app.setUpBeforeClosingApp();
and you can put this inside every onDestroy()
of Activity and from here code handling before closing actions can be achieved.
Do it in the onPause()
method which is called as soon as the activity is no more in the foreground.
First of all it is possible to detect the home key event in an indirect way as shown in my post about killing an application when the home key is pressed: How to close Android application?
Also it is possible to determine if an activity is the root activity in its onDestroy() method and then have it call a helper class to perform final processing if it is the root activity. Better yet would be to create a custom activity that all of your activities inherit from, place the final processing logic in the onDestroy() method of the custom activity and then have all subclasses call super.onDestroy() in their onDestroy() method. This is similar to what is done in the aforementioned post: How to close Android application? as well as posts about creating setting screens when the menu button is pressed as well as handling the search button and return button.
I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With