Is there any way to have one and only one instance of each activity?

后端 未结 8 1400
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 06:41

I\'m finding that in my application, the user can get quite \'nested\' in the various activities that are opened while the user is using the application.

For example

相关标签:
8条回答
  • 2020-12-08 07:39
    Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_HISTORY);
    

    It works for me :)

    0 讨论(0)
  • 2020-12-08 07:41

    Using android:launchMode="singleTask" is probably the best approach, since it won't recreate the activity if it's already running. Just add it to the activity in your AndroidManifest.xml, and you should be all set.

    <activity
        android:name=".MyActivity"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
     </activity>
    

    Here's another question that might be useful: Android singleTask or singleInstance launch mode?

    0 讨论(0)
提交回复
热议问题