2

My problem occurs with the following user behaviour:

  1. Open application
  2. Close application (using back button)
  3. Rotate from portrait to landscape (or from landscape to portrait)
  4. Open application

When the application is opened the second time, the onCreate method is executed twice because screen orientation has changed.

I have done some testing, and it seems like the problem occurs only with devices running versions older than Android 3.0 (I have not tested with every version).

On my tablet running Android 4.0, the application is opened in correct initial orientation on second start, causing the onCreate method to be called only once. But on my handset running Android 2.3.5 (HTC Desire HD), a second start opens the previous Activity instance in the wrong orientation, causing a restart and 2 executions of the main activity's onCreate method.

I have read Tasks and Back Stack | Android Developers and tried using various combinations of android:launchMode, android:clearTaskOnLaunch, etc, on the main activity. But that did not seem to be the trick.

Any suggestions? Has there been made any changes from Android 2.0 to 3.0 in the way applications are exited using the Back button? Or is this something that happens on only some devices, independent of Android version?

Activity:

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(getClass().getName(), "onCreate");
        setContentView(R.layout.main);
    }
}

Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello World, MyActivity" />
</LinearLayout>

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7"/>
    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name="MyActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>
Mr Auni
  • 708
  • 6
  • 10
  • Firstly don't confuse "application" or "app" with `Activity`. If you press the `BACK` button, you terminate the current `Activity` - this is not the same as the "app" or "application". Secondly, we'd need to see some code - if you're exiting the `Activity` then immediately rotating the device and restarting the `Activity`, depending on the 'exit' code in `onPause()`, `onStop()` and `onDestroy()`, the first instance of the `Activity` may still be running. – Squonk Aug 05 '12 at 22:31
  • Squonk: thank you for your answer. I have not overridden `onPause()`, `onStop()` or `onDestroy()`. Is there any way to force destruction of the first instance of the Activity? – Mr Auni Aug 06 '12 at 00:02

2 Answers2

0

I think the right solution to the problem would be to avoid doing a lot of stuff in Activity's onCreate method so orientation changes does not cause problems.

Have a look at this question: Activity restart on rotation Android

Alternatively, declaring android:configChanges="orientation" in the manifest should notify to OS that it should not destroy and re-create the activity on orientation changes and call onConfigurationChanged instead.

Community
  • 1
  • 1
Sergey
  • 11,892
  • 2
  • 41
  • 52
0

Sergey is right, if you add to your code

<activity android:name="MyActivity"
          android:label="@string/app_name"
          android:configChanges="orientation">

and

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(getClass().getName(), "onCreate");
    if(savedInstanceState == null) {
        setContentView(R.layout.main);
    }
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.main);

}

The onCreate will be executed only once when the activity is started.

BrainCrash
  • 12,992
  • 3
  • 32
  • 38