Skip to content Skip to sidebar Skip to footer

How To Detect If Whole Application Is Close In Android

Some times application be closed by pressing home button and onDestroy() doesn't call. I want to call a method when whole application is closed and I'm not going to call my method

Solution 1:

implements LifecycleObserver inside appication class an then use as blow:

public class App extends Application implements LifecycleObserver{

    @SuppressLint("CheckResult")
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onMoveToForeground() {


    }

    @SuppressLint("CheckResult")
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onMoveToBackground() {

    }
}

Also can use other events like Lifecycle.Event.ON_DESTROY or ON_CREATE


Solution 2:

Application does not close on home button press.But it goes in background.

When you Application goes in background(Your front activity goes in background) it calls onStop() Method(Activity is not visible now).So you should do all stuff here.

There is no such call back the Application class. Which tell you that application is destroyed. If you want to fire an event when application is fully closed.You should check your application's activity stack.If it does not have any activity than your application is closed. You should check it from a service.


Post a Comment for "How To Detect If Whole Application Is Close In Android"