活动创建两次

Nam*_*mNH 7 android screen-orientation activity-lifecycle

我设置了锁定方向

在此输入图像描述

并添加了2个简单类的示例代码,如下所示:

SplashLandscapeActivity.java

public class SplashLandscapeActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("start", "xxxx start Activity SplashLandscapeActivity");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(SplashLandscapeActivity.this, TestActivity.class));
                finish();
            }
        }, 500);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("start", "xxxx onDestroy Activity SplashLandscapeActivity");
    }
}
Run Code Online (Sandbox Code Playgroud)

TestActivity.java

public class TestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("start", "xxxx start Activity TestActivity "
                + getResources().getConfiguration().orientation);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("start", "xxxx onDestroy Activity TestActivity "
                + getResources().getConfiguration().orientation);
    }
}
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".SplashLandscapeActivity"
            android:theme="@style/SplashTheme"
            android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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

        <activity
            android:name=".TestActivity"
            android:screenOrientation="portrait"/>
    </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)

当我使用new Handler().postDelayed(SplashLandscapeActivity.java)开始时TestActivity,它开始两次,第一个有Landscape方向然后切换回portrait.日志显示了一切:

xxxx启动Activity SplashLandscapeActivity

xxxx start Activity TestActivity 2 // <== landscape

xxxx onDestroy活动TestActivity 1

xxxx start Activity TestActivity 1 // <== portrait

xxxx onDestroy活动SplashLandscapeActivity

如果我删除它Handler,TestActivity现在开始像正常的肖像.

xxxx启动Activity SplashLandscapeActivity

xxxx start Activity TestActivity 1

xxxx onDestroy活动SplashLandscapeActivity

所以,我的问题是:

1-此系统问题或其预期行为?为什么activity重启甚至screenOrientation被设置固定Manifest

2-实际上,我的真实项目没有任何Handler问题,只有activity两次开始的问题(开始之后Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK).我该如何处理这个问题?

小智 2

有 2 条注释可以防止 TestActivity 启动两次。希望对你有帮助

  1. 使用sensorPortrait而不是portrait在 TestActivity 中。TestActivity 不会启动两次,但它会旋转它以匹配用户握持设备的方式。
  2. 添加android:configChanges="keyboardHidden|orientation|screenSize" 到 Manifest.xml 中的 TestAcitivty。它将调用public void onConfigurationChanged(Configuration newConfig)而不是重新启动。

我在Android N中没有发现这个问题。