Search code examples
androidandroid-lifecycle

Detect Android Application Resume and Pause state


I want to start ScheduledExecutorService on resume of application and want to stop on pause of application.

I only found solution by detecting acivities running status by maintaining count. Like in lib https://github.com/curioustechizen/android-app-pause/tree/master/android-app-pause .

Is there is any other solution to detect application pause and resume state?


Solution

  • You should use ProcessLifecycleOwner.

    Class that provides lifecycle for the whole application process.

    You can consider this LifecycleOwner as the composite of all of your Activities, except that ON_CREATE will be dispatched once and ON_DESTROY will never be dispatched. Other lifecycle events will be dispatched with following rules: ProcessLifecycleOwner will dispatch ON_START, ON_RESUME events, as a first activity moves through these events. ON_PAUSE, ON_STOP, events will be dispatched with a delay after a last activity passed through them. This delay is long enough to guarantee that ProcessLifecycleOwner won't send any events if activities are destroyed and recreated due to a configuration change.

    It is useful for use cases where you would like to react on your app coming to the foreground or going to the background and you don't need a milliseconds accuracy in receiving lifecycle events.

    Implementation

    Step 1. Create a class named MyApp that extends from the Application class.

    public class MyApp extends Application {
    
        @Override
        public void onCreate() {
            super.onCreate();
            ProcessLifecycleOwner.get()
                    .getLifecycle()
                    .addObserver(new ProcessLifecycleObserver());
        }
    
        private static final class ProcessLifecycleObserver implements LifecycleObserver {
    
            @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
            public void onApplicationResumed() {
                // Start ScheduledExecutorService here
            }
    
            @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
            public void onApplicationPaused() {
                // Stop ScheduledExecutorService here
            }
        }
    }
    

    Step 2. Add the class into the AndroidManifest.xml file

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.kotlinapp">
    
        <application
            android:name=".MyApp"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
    
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    </manifest>