Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity created twice

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?

like image 370
NamNH Avatar asked May 22 '17 03:05

NamNH


People also ask

What is a second activity?

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.

How do I know if my android activity is recreated?

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. } }


2 Answers

In your manifest file edit the TestActivity block like this

<activity android:name=".TestActivity" android:launchMode="singleInstance" android:screenOrientation="portrait"/>

like image 71
Shubham Goel Avatar answered Oct 29 '22 19:10

Shubham Goel


There are 2 comments to prevent TestActivity start twice. Hope Help you

  1. use sensorPortrait instead of portraitin 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.
  2. add 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.

like image 23
VictorPanda Avatar answered Oct 29 '22 18:10

VictorPanda