Activity 启动另一个全屏时未调用 onStop() Activity

Activity's onStop() not called when starting another full screen Activity

我曾经以为当ActivityA被另一个全屏ActivityB替换时,A的onStop()回调就会被调用

这也反映在文档中:

The visible lifetime of an activity happens between the call to onStart() and the call to onStop(). During this time, the user can see the activity on-screen and interact with it. For example, onStop() is called when a new activity starts and this one is no longer visible.

但是,现在我观察到不同的行为(在 Lollipop 和 Marshmallow 上测试过)。

我从HomeActivity开始AuthenticationActivity,尽管AuthenticationActivity是全屏activity,但HomeActivity并没有停止。

在清单中声明这些活动:

    <activity
        android:name=".screens.home.activities.HomeActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <activity
        android:name=".screens.authentication.activities.AuthenticationActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:windowSoftInputMode="adjustResize"
        android:theme="@style/AppTheme.Transparent"/>

当活动切换时,这是我在 logcat 中观察到的:

11-28 10:16:31.443 15183-15183/somepackage I/LOG:: LifeCycle : activity (somepackage.screens.home.activities.HomeActivity@3561e8e1) paused
11-28 10:16:31.583 15183-15183/somepackage I/LOG:: LifeCycle : activity (somepackage.screens.authentication.activities.AuthenticationActivity@2bbdb20f) created
11-28 10:16:31.753 15183-15183/somepackage D/Activity: performCreate Call secproduct feature valuefalse
11-28 10:16:31.753 15183-15183/somepackage D/Activity: performCreate Call debug elastic valuetrue
11-28 10:16:31.753 15183-15183/somepackage D/AuthenticationActivity: onStart()
11-28 10:16:31.753 15183-15183/somepackage I/LOG:: LifeCycle : activity (somepackage.screens.authentication.activities.AuthenticationActivity@2bbdb20f) started
11-28 10:16:31.993 15183-15183/somepackage D/AuthenticationActivity: onResume()
11-28 10:16:31.993 15183-15183/somepackage I/LOG:: LifeCycle : activity (somepackage.screens.authentication.activities.AuthenticationActivity@2bbdb20f) resumed
11-28 10:16:32.213 15183-15183/somepackage I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@266569db time:354918367
11-28 10:16:32.613 15183-15183/somepackage I/LOG:: LifeCycle : activity (somepackage.screens.home.activities.HomeActivity@3561e8e1) saveInstanceState
11-28 10:16:32.633 15183-15183/somepackage V/ActivityThread: updateVisibility : ActivityRecord{2bcd65fd token=android.os.BinderProxy@86da390 {somepackage/somepackage.screens.home.activities.HomeActivity}} show : true

怎么回事?

"For example, onStop() is called when a new activity starts and this one is no longer visible."

因为你的 AuthenticationActivity 是透明的,HomeActivity 仍然可见 => onStop 没有被调用

尝试删除:

android:theme="@style/AppTheme.Transparent"