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?
A secondary activities is an activity performed by a unit in addition to its principal activity. The result of a secondary activity is called secondary production.
You can determine if the activity is finishing by user choice (user chooses to exit by pressing back for example) using isFinishing() in onDestroy . @Override protected void onDestroy() { super. onDestroy(); if (isFinishing()) { // wrap stuff up } else { //It's an orientation change. } }
In your manifest file edit the TestActivity block like this
<activity
android:name=".TestActivity"
android:launchMode="singleInstance"
android:screenOrientation="portrait"/>
There are 2 comments to prevent TestActivity start twice. Hope Help you
sensorPortrait
instead of portrait
in the TestActivity. And the TestActivity will not start twice, but it will rotate it to match the way in which the device is held by the user.android:configChanges="keyboardHidden|orientation|screenSize"
to the TestAcitivty in your Manifest.xml.It will call public void onConfigurationChanged(Configuration newConfig)
instead of restart.I have not found this issue in Android N.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With