My application contains a main activity A and several other activities B, C, D, E that can be started only from the main activity A via the Menu.
For example if I’m in activity B and I click the Back button, then I always come back to the main activity A.
Considering again we are in activity B and I click the Home button. Then I have different behavior when clicking the launch icon of my app to return to the app :
- On Gingerbread (tested on Emulator) this cause activity B to display and I’m able to go back to activity A be pressing the Back button.
- On ICS the behavior is different, this cause a new activity A to start and if I click the back button in A then I come back to B. This not the expected behavior, the right one for me is the Gingerbread one.
Another example, if I’m in the main activity A and I click the Home button. Then I click the launch icon :
- On Gingerbread this cause activity A to resume. So if I click on the back button I exist from the app.
- On ICS this cause a new activity A to be created. So if I click on the back button I come back to the previous activity A, and I have to click again on A to exit.
Here is the manisfest.xml content :
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="10"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.android.vending.CHECK_LICENSE" />
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"/>
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".B"
android:configChanges="orientation"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".C"
android:configChanges="orientation"
android:screenOrientation="landscape">
</activity>
<activity
android:name=".D"
android:configChanges="orientation"
android:screenOrientation="landscape">
</activity>
<activity
android:name=".E"
android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.perfexpert.intent.ACTIVITY_E" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".A"
android:configChanges="orientation"
android:screenOrientation="portrait"
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>
And the code used to start activities from the main activity :
startActivityForResult(new Intent(this, B.class), REQUEST_CODE_B);
Why do I have this different behavior on ICS ? How to get the Gingerbread behavior on ICS ?
According to the Android Developers site the expected is the following :
Suppose, for example, that the current task (Task A) has three activities in its stack—two under the current activity. The user presses the Home button, then starts a new application from the application launcher. When the Home screen appears, Task A goes into the background. When the new application starts, the system starts a task for that application (Task B) with its own stack of activities. After interacting with that application, the user returns Home again and selects the application that originally started Task A. Now, Task A comes to the foreground—all three activities in its stack are intact and the activity at the top of the stack resumes.
This is the behavior I get on my Emulator (Gingerbread) but not on my Nexus S (ICS).
Thanks
I’m going to assume that you started the app initially (the first time) from an IDE (like Eclipse or IntelliJ) or after installing it using the installer (from the market or browser or clicking on APK in file browser). If so, this is a known bug in Android (see http://code.google.com/p/android/issues/detail?id=26658 ). Many people have struggled for days chasing this problem 🙁
A simple workaround for this problem can be found at http://code.google.com/p/android/issues/detail?id=2373#c21
To verify this is your problem, don’t start it from the IDE or installer. Simply install the app and then go start it from the list of available applications.
The bug is there on all devices, on all versions of Android (at least up to ICS, haven’t tested on JellyBean yet). It all works as it should in the emulator, so you cannot use emulator behaviour as an indication of real device behaviour.