Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Application On Exit Event [closed]

Tags:

android

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. :-(

like image 544
Black Avatar asked Aug 18 '10 11:08

Black


People also ask

How do I stop an Android application from closing?

In android, by pressing a back button or home button. So put an event key listener for back & home button and terminate the service.

How do I see which apps are closed on Android?

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.

How do you check app is killed or not in Android?

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.


3 Answers

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.

like image 71
y ramesh rao Avatar answered Oct 02 '22 13:10

y ramesh rao


Do it in the onPause() method which is called as soon as the activity is no more in the foreground.

like image 33
WarrenFaith Avatar answered Oct 02 '22 13:10

WarrenFaith


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.

like image 36
Danny Remington - OMS Avatar answered Oct 02 '22 14:10

Danny Remington - OMS