简体   繁体   English

Android - 为什么我的应用程序恢复时我的活动会被销毁?

[英]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.我在 onDestroy() 函数上放置了一个断点,因为我找不到在它之前调用的任何代码。 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.在 Looper 中有一个名为 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:编辑:根据要求,这里是 AndroidManifest.xml 文件:

    <?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>

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.您已在清单中将 Main 活动设置为SingleTask模式,因此当您启动它的新实例时,该活动将被销毁。 Here below an interesting article about SingleMode and launch mode:下面是一篇关于 SingleMode 和启动模式的有趣文章:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android 返回活动时它被破坏并且不恢复 - android when returning to activity it is destroyed and not resumed Android-我的活动被销毁了吗? - Android - Wh is my activity being destroyed? 在Android中,如何检测为什么我的活动得以恢复? - In Android, how can I detect why my activity was resumed? 当我结束使用顶部活动时,为什么我的主要活动被重新创建而不恢复? - Why does my main activity get recreated and not resumed when I finish using the top activity? 为什么我的Android应用程序销毁后,我的列表数据为空? - Why is my List data null after my android app is destroyed? Android:我的应用会被系统破坏吗? - Android: Will my app be destroyed by system? 为什么恢复后我的活动又从顶部开始而不是什么也不做? - Why is my Activity runs from the top again when resumed instead of doing nothing? 为什么我的android活动被停止了? - Why is my android activity being stopped? 为什么长时间在后台运行时,我的android应用程序的所有静态变量都会被破坏? - Why my android application's all static variables are destroyed when the app is in background for a long period of time? 为什么我的启动服务被销毁? - Why is my started service being destroyed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM