My problem occurs with the following user behaviour:
- Open application
- Close application (using back button)
- Rotate from portrait to landscape (or from landscape to portrait)
- 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>