2

I'm having a problem where multiple instances of an activity are present in the backstack.

e.g.

Stack: A > B > C Press home and use some other apps then relaunch my app Stack: A > B > C > A

I would expect my app to resume with activity C, which in most cases it does, but occasionally it resumes with activity A and then when i press back it goes to activity C.

I know this problem is not caused by android killing my app as if this was the case it would resume with activity A as the root.

I have tried altering the launchMode attribute to singleTask and singleInstance but this results in activity A being resumed as the root, which is not the behaviour that i want, i want it to go back to the previously viewed activity with the backstack intact (I understand this is not always possible if the OS kills the app, which is fine).

I dont believe setting the launchMode attribute to singleTop would be useful either as this would prevent A > A situations but not A > B > A or A > B > C > A etc

Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package=""
  android:versionCode="1"
  android:versionName="1.0"
  android:installLocation="auto"
    >

<application android:label="@string/app_name" android:debuggable="false"
        android:name=""
        android:icon="@drawable/icon" android:theme="@style/CustomTheme"
        android:targetSdkVersion="8" android:minSdkVersion="4">

    <activity android:name=".activity.A"
            android:windowSoftInputMode="stateUnchanged|adjustPan"
            android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
            </activity>
    <activity android:name=".activity.B"
            android:screenOrientation="portrait"/>
    <activity android:name=".activity.C"
            android:screenOrientation="portrait" />
    <activity android:name=".activity.D"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
    <activity android:name=".activity.E"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />

    <service android:name=".service.S" />

    <uses-library android:name="com.google.android.maps" />


</application>

<uses-sdk android:minSdkVersion="4"
      android:targetSdkVersion="8"
        />

 <supports-screens
    android:largeScreens="false"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"
         />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Any advice would be greatly appreciated

David

user648462
  • 645
  • 9
  • 23
  • How do you launch Activity A? Is it the main activity/launcher activity? – whlk Jul 19 '11 at 10:54
  • Yes its the main/launcher activity: `code` `code` – user648462 Jul 19 '11 at 12:28
  • Does any of the other Activities change the default App Stack behaviour? – whlk Jul 19 '11 at 12:48
  • No, they dont specify any non default behaviour – user648462 Jul 19 '11 at 12:56
  • Could you post the AndroidManifest.xml? – user802421 Jul 19 '11 at 12:58
  • [http://stackoverflow.com/questions/2441203/how-to-make-an-android-app-return-to-the-last-open-activity-when-relaunched/2441745#2441745][1] [1]: http://stackoverflow.com/questions/2441203/how-to-make-an-android-app-return-to-the-last-open-activity-when-relaunched/2441745#2441745 – Pratik Jul 19 '11 at 13:16
  • [http://stackoverflow.com/questions/2441203/how-to-make-an-android-app-return-to-the-last-open-activity-when-relaunched/2441745#2441745][1] [1]: http://stackoverflow.com/questions/2441203/how-to-make-an-android-app-return-to-the-last-open-activity-when-relaunched/2441745#2441745 – Pratik Jul 19 '11 at 13:17
  • check [this][1] post [1]: http://stackoverflow.com/questions/2441203/how-to-make-an-android-app-return-to-the-last-open-activity-when-relaunched/2441745#2441745 – Pratik Jul 19 '11 at 13:19
  • The manifest seem fine. Are you doing something in onPause, onStop, onDestroy methods in any of A-E activities? – user802421 Jul 19 '11 at 13:35
  • I am unbinding from a service in the onStop of some of the activities – user648462 Jul 19 '11 at 14:24
  • What i dont understand is why is this behaviour intermittent, most of the time it works exactly how i would expect it by either going to the last viewed activity with backstack intact or by going to the main activity with no backstack as the app was killed by the OS. Its just on the odd occasion that it starts a new main activity on top of the existing apps stack. It happens much more often with development builds when im constantly overwriting the app from my IDE but it still happens occasionally with release builds. – user648462 Jul 20 '11 at 08:53

1 Answers1

1

The behavior you're looking for is what singleTask was meant for.

The problem, then, is why resuming your app pops back to the root. If it's just due to the usual way the system works (e.g., 30-minute timeout), and you really want to prevent that, you could use android:alwaysRetainTaskState="true".

usethe4ce
  • 23,261
  • 4
  • 30
  • 30