Activity created twice

Viewed 2772

I have set locked orientation

enter image description here

and added the sample code with 2 simple classes like below:

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

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

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>

When I use new Handler().postDelayed (SplashLandscapeActivity.java) to start TestActivity, It's started twice, the first one has Landscape orientation then switch back to portrait. The log showed it all:

xxxx start Activity SplashLandscapeActivity

xxxx start Activity TestActivity 2 // <== landscape

xxxx onDestroy Activity TestActivity 1

xxxx start Activity TestActivity 1 // <== portrait

xxxx onDestroy Activity SplashLandscapeActivity

And if I remove that Handler, TestActivity now started with portrait like normal.

xxxx start Activity SplashLandscapeActivity

xxxx start Activity TestActivity 1

xxxx onDestroy Activity SplashLandscapeActivity

So, my question is:

1- Is this system issue or its intended behavior? Why activity is restarted even the screenOrientation was set fixed in Manifest?

2- Actually, my real project do not have any Handler but has the same issue that activity started twice (after start with Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK). How can I deal with this issue?

5 Answers
Related