onStop()没有被调用?

aks*_*hay 5 android activity-lifecycle android-activity

我正在尝试获取Activity Lifecycle的日志.我在这里面临一些奇怪的问题.

当我使用活动的主题时android:theme="@style/Theme.AppCompat.Light.NoActionBar,转到下一个活动.onPuase()onStop()被调用.

但是,当我使用时android:theme="@style/AppTheme,onPause()被调用,但onStop()不会被调用.

是否有基于活动主题的活动?

您可以参考以下代码.

Styles.xml

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="android:windowIsTranslucent">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
Run Code Online (Sandbox Code Playgroud)

的Manifest.xml

    <activity
        android:name=".activity.TestActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity
        android:name=".activity.TestTwoActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme" />
Run Code Online (Sandbox Code Playgroud)

测试活动

public class TestActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    Logger.debug("TestActivity onCreate");
    findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(TestActivity.this, TestTwoActivity.class);
            startActivity(intent);


        }
    });
}

@Override
protected void onStart() {
    Logger.debug("TestActivity onStart");
    super.onStart();
}

@Override
protected void onRestart() {
    Logger.debug("TestActivity onRestart");
    super.onRestart();
}

@Override
protected void onResume() {
    Logger.debug("TestActivity onResume");
    super.onResume();
}

@Override
protected void onPause() {
    Logger.debug("TestActivity onPause");
    super.onPause();
}

@Override
protected void onStop() {
    Logger.debug("TestActivity onStop");
    super.onStop();
}

@Override
protected void onDestroy() {
    Logger.debug("TestActivity onDestroy");
    super.onDestroy();
}
Run Code Online (Sandbox Code Playgroud)

}

activity_test

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical">

<Button
    android:id="@+id/btn"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@color/bg_blue"
    android:text="button" />
Run Code Online (Sandbox Code Playgroud)

` activity_test_two

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">

<Button
    android:id="@+id/btn"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_centerInParent="true"
    android:background="@color/bg_blue"
    android:text="button" />
Run Code Online (Sandbox Code Playgroud)

日志:

 TestActivity onCreate
 TestActivity onStart
 TestActivity onResume
     activity Button Click
 TestActivity onPause
 TestTwoActivity onCreate
 TestTwoActivity onStart
 TestTwoActivity onResume
    Backpress
 TestTwoActivity onPause
 TestActivity onResume
 TestTwoActivity onStop
 TestTwoActivity onDestroy
Run Code Online (Sandbox Code Playgroud)

Tom*_*ura 6

您的第二个活动是半透明的 - 这意味着,第一个活动仍然可见,因此onStop()不会被调用.这与显示对话框非常相似 - onPause()因为活动不在前台但仍然对用户可见而被onStop()调用- 所以没有调用


aks*_*hay 0

<item name="android:windowIsTranslucent">true</item>不会更改 Activity 的生命周期。

我不知道我的案例出了什么问题。重新启动工作室解决了该问题。