Search code examples
androidandroid-activityandroid-lifecycle

Android - Why is my activity being destroyed when my app is resumed?


It only gets destroyed if I return to my app by tapping the icon. If I open the list of running apps and return to it from there, it resumes normally. I put a breakpoint at the onDestroy() function, because I can't find any of my code that gets called before it. Here's the thread it was called on:

onDestroy:500, CookActivity {com.mycompany.myapp}
performDestroy:6228, Activity {android.app}
callActivityOnDestroy:1157, Instrumentation {android.app}
performDestroyActivity:3898, ActivityThread {android.app}
handleDestroyActivity:3929, ActivityThread {android.app}
access$1400:160, ActivityThread {android.app}
handleMessage:1375, ActivityThread$H {android.app}
dispatchMessage:102, Handler {android.os}
loop:135, Looper {android.os}
main:5597, ActivityThread {android.app}
invoke:-1, Method {java.lang.reflect}
invoke:372, Method {java.lang.reflect}
run:984, ZygoteInit$MethodAndArgsCaller {com.android.internal.os}
main:779, ZygoteInit {com.android.internal.os}

It looks like it's receiving some message to destroy my activity from somewhere. In the Looper there's a variable called msg. This is it's value:

{ when=-5m57s667ms what=109 arg1=1 obj=android.os.BinderProxy@1433d777 target=android.app.ActivityThread$H }

Does anyone know what's happening here? How do I make my app resume normally like it does when I select it from the running app list?

Edit: As requested, here is the AndroidManifest.xml file:

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />


    <uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        android:name=".App"
        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"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:theme="@style/MainTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".CookActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/title_activity_cook"
            android:launchMode="singleTop"
            android:parentActivityName=".MainActivity">
        </activity>
        <activity
            android:name=".AlarmActivity"
            android:excludeFromRecents="true"
            android:showOnLockScreen="true"
            android:showForAllUsers="true"
            android:theme="@style/AlarmTheme">
        </activity>

        <service android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
        <service android:name="org.eclipse.paho.android.service.MqttService" />

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

    </application>

</manifest>

Solution

  • You have set the Main activity on SingleTask mode in the manifest, so the activity will be destroyed when you launch a new instance of it. Here below an interesting article about SingleMode and launch mode:

    https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en