0

I want to detect when my application paused (when the user press the home button or starts another application). Creating application class with implementing lifecycle not working,because it's working for every activity switch. And i cant not call onpaused and onstop everywhere. So how can i detect this.

Kittu
  • 11
  • 3

2 Answers2

2

Now while the app is minimized, you can detect the event easily in Application class, using 'ProcessLifecycleOwner' instead of tracking it in every page. Do the following changes:

build.gradle (App-Level):

implementation "android.arch.lifecycle:extensions:1.0.0"

Create an application class (if not created already) and implement the code as below:

class MainApplication extends Application implements LifecycleObserver {
    @Override
    public void onCreate() {
        super.onCreate();
        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onAppBackgrounded() {

        Log.d("APPP","APP BACKGROUNDED");

    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onAppForegrounded() {
        Log.d("APPP","APP FOREGROUNDED");
    }
}
Yamini Balakrishnan
  • 2,361
  • 16
  • 24
0

To detect when your application moves from background to foreground or foreground to background, you can use ProcessLifecycleOwner.

Check this Answer. The implementation of this was explained very clearly.

Pavan Varma
  • 1,199
  • 1
  • 9
  • 21