Search code examples
androidandroid-lifecycleoncreateapplication-lifecycleandroid-ondestroy

Why is (and how to fix) my Android application triggering an onCreate (ON_CREATE) when removed from memory?


I manage an Android app for a client and am trying to detect when the app is removed from memory. I've noticed via logcat that when the app is removed from memory an ON_CREATE Lifecycle.Event is being sent. I do get an ON_STOP when the app is closed, but swiping to remove it completely from memory for some reason only generates an ON_CREATE that is additional to the original one at app launch.

I have some code that runs in Application object's ON_CREATE and can prevent it from being executed a second time if I want, but would rather prevent this extraneous event from firing. This seems wrong and I want to fix it, and get an ON_DESTROY if I can, although I've seen here on SO that sometimes it is not fired.

My Application class code looks like this:

public class ThisApplication extends Application implements LifecycleObserver
{    
    @Override
    public void onCreate() {
        super.onCreate();
        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
        Log.e("ThisApplication", "Inside onCreate()");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onAppStop() {
        Log.e("ThisApplication", "ON_STOP()");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onAppStart() {
        Log.e("ThisApplication", "ON_START()");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onAppDestroy() {
        Log.e("ThisApplication", "ON_DESTROY");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onAppResume() {
        Log.e("ThisApplication", "ON_RESUME");    
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void onAppCreate() {
        Log.e("ThisApplication", "ON_CREATE");
    }

}

As requested here is the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.neimander.locus_android">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <uses-feature
        android:name="android.hardware.camera"
        android:required="true"/>
    <uses-feature android:name="android.hardware.camera.autofocus"/>

    <uses-permission android:name="android.permission.CAMERA"/>

    <application

        android:hardwareAccelerated="true"
        android:allowBackup="false"
        android:icon="@drawable/ail_logo"
        android:label="@string/app_name"
        android:theme="@style/MainTheme"
        android:name="ThisApplication">

        <service android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService" android:enabled="true" />
        <activity
            android:windowSoftInputMode="stateVisible"
            android:name=".LoginActivity"
            android:excludeFromRecents="true"
            android:label="Login"
            android:screenOrientation="portrait"/>
        <activity
            android:name="com.microsoft.aad.adal.AuthenticationActivity"
            android:label="Authentication" >
        </activity>
        <activity
            android:name=".HomeScreenActivity"
            android:label="@string/title_activity_home_screen"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".GradeAssessmentActivity"
            android:label="@string/title_activity_grade_assessment"
            android:noHistory="true"
            android:parentActivityName=".HomeScreenActivity"
            android:screenOrientation="portrait"/>
        <activity
            android:name=".ScanningActivity"
            android:label="@string/title_activity_scanning"
            android:parentActivityName=".GradeAssessmentActivity"
            android:screenOrientation="portrait"/>

    </application>

</manifest>

Solution

  • In the ProcessLifecycleOwner doc, it says:

    ON_CREATE will be dispatched once and ON_DESTROY will never be dispatched

    So if ON_CREATE is dispatched more than once, it is a bug and you should report it to the Android Issue Tracker.

    But before jumping on the bug report train, you should double check that you are registering and observing the lifecycle properly. It is more than likely that there is a silly error in the code.

    Also, it is impossible to track when your app is removed from memory from within the app itself. Android may keep your application's process even when nothing is in it. Opposite to that, Android may kill your application's process with no warning.