android - onStop() 没有被调用?

标签 android android-activity activity-lifecycle

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

当我将 Activity 的主题用作 android:theme="@style/Theme.AppCompat.Light.NoActionBar 并转到下一个 Activity 时。onPuase()onStop() 被调用。

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

是否有基于 Activity 主题的 Activity ?

您可以引用下面的代码。

样式.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>

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

测试 Activity

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

最佳答案

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

关于android - onStop() 没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42504327/

相关文章:

android - 何时应该使用 LRUCache 回收位图?

android - Android Studio Gradle连接被拒绝:连接

android - 完成()不工作

android - 防止新 Intent 或切换应用程序破坏 Activity

android:Activity完成后Asynchtask的onPostExecute

android - 使用 BottomAppBar 或 BottomNavigationView 创建自定义底部 View

xml - 幻象错误 "error parsing XML: unbound prefix"

android - 首次启动时首次加载时, Activity 完成得太快

android - android中的圆形布局

android - View 什么时候获得它的边界?