简体   繁体   中英

Android multiple instances of Activity

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

eg

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

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 (eg, 30-minute timeout), and you really want to prevent that, you could use android:alwaysRetainTaskState="true" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM