onStop()方法没有被调用?

5

我正在尝试获取Activity生命周期的日志,但是我遇到了一些奇怪的问题。

当我将activity的主题设置为android:theme="@style/Theme.AppCompat.Light.NoActionBar并进入下一个activity时,onPause()onStop()会被调用。

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

是否有基于Activity主题的事件?

您可以参考以下代码。

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>

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" />

TestActivity

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();
}

}

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" />

`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" />

日志:

 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
3个回答

6

你的第二个活动是半透明的 - 这意味着第一个活动仍然可见,因此不会调用onStop()。这非常类似于显示对话框 - onPause()被调用,因为活动不在前台,但仍可见于用户 - 因此不会调用onStop()


从主题中删除了<item name="android:windowIsTranslucent">true</item>,但仍然面临相同的问题。 - akshay
你能贴出你的活动布局吗? - Tomasz Czura
谢谢你的回答,Tomasz。windowIsTranslucent不会影响活动生命周期。我重新启动了系统,现在它可以正常工作了 :( - akshay
由于这个问题已经不再有效,我将删除此问题。 - akshay

0

检查您是否已将windowIsTranslucent设置为true,用于您的第二个活动。如果设置了,则onStop()不会在您的第一个活动上调用。


0

<item name="android:windowIsTranslucent">true</item> 不会对 Activity 的生命周期造成影响。

我不知道我的情况出了什么问题。重启 Studio 解决了这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接