Android Activity的多个实例

发布于 2024-11-25 12:21:17 字数 2650 浏览 2 评论 0原文

我遇到一个问题,即后台堆栈中存在多个活动实例。

例如

堆栈:A> B> C 按主页并使用其他一些应用程序,然后重新启动我的应用程序 堆栈:A> B> C> A

我希望我的应用程序能够以活动 C 恢复,在大多数情况下确实如此,但偶尔它会以活动 A 恢复,然后当我按回时,它会转到活动 C。

我知道这个问题不是由 android 杀死我的应用程序引起的就好像这种情况一样,它将以活动 A 作为根恢复。

我尝试将 launchMode 属性更改为 singleTask 和 singleInstance,但这会导致活动 A 作为根恢复,这不是我想要的行为,我希望它返回到先前查看的活动,且后堆栈完好无损(我理解如果操作系统杀死了应用程序,这并不总是可能的,这很好)。

我不认为将 launchMode 属性设置为 singleTop 会有用,因为这会阻止 A > 。 A情况但不是A> B> A或A> B> C>等清单

<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" />

任何建议将不胜感激

大卫

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白况 2024-12-02 12:21:17

您正在寻找的行为正是 singleTask 的目的。

那么,问题是为什么恢复应用程序会弹出到根目录。如果这只是由于系统的正常工作方式(例如,30 分钟超时)引起的,并且您确实想阻止这种情况,则可以使用 android:alwaysRetainTaskState="true"

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文