Hey I have an application where I want to now when the APP goes to onPause or onDestroy because I want to call a certain function when this happens. I tried to override the onPause in an activity and extended that activity in all project but the onPause was being called on every migration between activities (which is logical) but this is not what I want. I want to know when the user exits the app or pauses it (pressing the home button) Regards,
-
You want to listen to `onStop()` rather than `onPause()`. – Murat Karagöz Mar 03 '17 at 10:16
-
1Define "user exits the app" .. There is no such concept in android. **edit:** you may check `isFinishing()` inside `onPause` maybe it's what you want – Selvin Mar 03 '17 at 10:16
-
what i meant is by exiting the app is presseing the home button (from any activity), or exiting the normal way using the back button. in the case of the back button it's easy i can override the onstop in the master activity but what about pressing the home from any activity – Hussein M. Yassine Mar 03 '17 at 10:20
-
Pressing home button is more like minimize button in Windows OS.. it's not exit the app – Selvin Mar 03 '17 at 10:24
-
There is no "Exit App" in Android but this question discusses at length the "Backgrounding" of an app: http://stackoverflow.com/questions/4414171/how-to-detect-when-an-android-app-goes-to-the-background-and-come-back-to-the-fo – Kuffs Mar 03 '17 at 10:25
6 Answers
Pull this dependency in your build.gradle file:
dependencies {
implementation "android.arch.lifecycle:extensions:1.1.1"
}
Then in your Application class, use this:
public class MyApplication extends Application implements LifecycleObserver {
@Override
public void onCreate() {
super.onCreate();
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
private void onAppBackgrounded() {
Log.d("MyApp", "App in background");
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
private void onAppForegrounded() {
Log.d("MyApp", "App in foreground");
}
}
Update your AndroidManifest.xml file:
<application
android:name=".MyApplication"
....>
</application>

- 190
- 1
- 8
-
One thing to keep in mind is that if the user closes the app too quickly (like open the task switch interface and then swipe up without stop) the function will not be triggered as the app is killed already. – DrustZ Feb 16 '21 at 06:08
in all of your activities :
@Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
Log.e("TAG", "Activity Minimized");
}
in your main activity :
@Override
public void onBackPressed() {
Log.e("TAG", "App Exit");
super.onBackPressed();
}
note : there is no way to detect when app killed by system ( for example quit with task manager )

- 12,955
- 8
- 43
- 65
-
Activity A=> starts Activity B ... non of those would be called ... but Activity A would be "minimized" – Selvin Mar 03 '17 at 10:31
-
yes, I read ... but it seems you don't know the Activity/Application lifecycle ... App may be killed in second activity then "App Exit" is never called and app is dead ... The only answer is "there is no concept of app exit in android" – Selvin Mar 03 '17 at 10:34
-
@Selvin i know, but there is no way to detect system termination of app – DJafari Mar 03 '17 at 10:35
I use this method to detect if the app goes to background or is killed
step 1: Make a service like this
public class OnClearFromRecentService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("ClearFromRecentService", "Service Started");
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("ClearFromRecentService", "Service Destroyed");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.e("ClearFromRecentService", "END");
//Code here
stopSelf();
} }
step 2)register this service in manifest.xml
<service android:name="com.example.OnClearFromRecentService" android:stopWithTask="false" />
step 3) Then start this service on your splash activity
startService(new Intent(getBaseContext(), OnClearFromRecentService.class));
So now when your app is manually killed, the onDestroy method will be called
Try this on your code and let me know if its working. Happy Coding :)

- 1,716
- 17
- 32
If the user exits the application the onDestroy() callback will be called, if the user presses the home button the onStop() callback will be called.

- 784
- 1
- 9
- 20
-
what if i was another activity other the one these two are implemented – Hussein M. Yassine Mar 03 '17 at 10:19
-
You can create a base activity and all your activities will inherite from that one and that way you can detect the callback's from any activity. – Nelson Almendra Mar 03 '17 at 10:21
-
Even if you have all activities extended from BaseActivity with callbacks, it doesn't matter. You finish one activity and start another. How can you recognize when the application exits? – CoolMind Feb 09 '21 at 07:52
So you want to know when the whole Application goes into Bacground, the method from Activity.onPause tells you when a single abtivity goes into background.
For your needs you could add
to your android manifest. this will call a class like this with name "YourApplication"
public class YourApplication extends Application {
@Override
public void onCreate() {}
public public void onTerminate() {
super.onTerminate();
}
}
e.g. for "onCreate" or "onTerminate" of the whole App (not for every single Activity). But java documentation say you should not trust that onTerminate is called on every app destroy (see: android.app.Application subclass, onTerminate is not being called).
I Think the best way is, if you
- add a own Methode "onPause" and "onResume" to your Application class
- in ALL your Activities you store into Application class when onPause or onResume is called
- and you call you own Application.onPause when ALL of you activities are in "onPause"
- and you call you Application.onResume when first Activity is back in "onResume"
Implement
Application.ActivityLifecycleCallbacks
in your Application Class. It will implement the following ActivityLifeCycle Methods :
isActivityVisible()
onActivityCreated()
onActivityStarted()
onActivityResumed()
onActivityPaused()
onActivityStopped()
onActivitySaveInstanceState()
onActivityDestroyed()
And Register the callback listener in OnCreate() of application class like :
registerActivityLifecycleCallbacks(this);
Define a Global variable in application class.
private static boolean isActive;
Now change the activity life cycle's overridden method like this:
public static boolean isActivityVisible() {
return isActive;
}
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
isActive = true;
}
@Override
public void onActivityPaused(Activity activity) {
isActive = false;
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
}
@Override
public void onActivityDestroyed(Activity activity) {
isActive = false;
}
and you can perform the required operations on the value of isActive variable.

- 1,453
- 17
- 30