I have set locked orientation
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?
activity
never started twice. – IlltemperedTestActivity
? or does it directly jump toTestActivity
in portrait without rotating? My guess is this could be due to the config changes being called after the activity hasonCreate
and probablyonResume
executed, and if no config changes happened like when skipping theSplashLandscapeActivity
and directly startingTestActivity
it wouldn't be called hence it wouldn't restartTestActivity
. – LudwiggonConfigurationChanged
is never called onTestActivity
, even if it was restarted by system (I guessed). – IlltemperedonSaveInstanceState
fromonCreate
method, second recreation of activity will have something different than null. – Ambiencewhy activity is restarted even the screenOrientation was set fixed in Manifest
? – Illtemperedactivity
is rotation successful, but my UI and logic code are failed becauseactivity
was initialized twice (onCreate, onResume
) – Illtempered