How to start Activity with FLAG_REORDER_TO_FRONT and FLAG_CLEAR_TOP

I have four actions in my tasks A, B, C, D.

Events are triggered in the order A-> B-> C-> D.

Here

  • I want to return to activity A from D and resume this activity.
    So I used the flag of intent

    i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
  • Actions B, C, D are no longer needed after stmt 1.
    I go for the flag to accomplish this,

     Intent.FLAG_ACTIVITY_CLEAR_TOP 

In my application, using the above 1 and 2, I try to achieve, for example, go back and resume activity A and remove other actions from the stack
so i tried.

  i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //vise versa 

using the code above, both flags are added here using this link
( Android: what's the difference between setFlags and addFlags for intent )

I cannot complete this task together (resume action A and clear another).

actual call script

 when i use the CLEAR flag the call is like D->oncreate(A) and clear BCD when i use the REORDER flag the call is like D->onrestart(A). 

So how can I combine these flags to get a combined action, to resume A and clear the others, or is there any other way to do this.

this is my manifest

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tpv.vptest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".NeverStopActivity" android:label="@string/title_activity_main" android:launchMode="singleInstance"> <intent-filter> <action android:name="android.intent.action.MAIN" /> 

activity 1-> 2

 Intent i = new Intent(getApplicationContext(), TopMenuBar.class); startActivity(i); 
  • You can complete this action in 1 second - repeat / cancel

activity 2-> 3

 Intent i = new Intent(getApplicationContext(), Activity3.class); startActivity(i); 

and 3-> 1

 Intent i = new Intent(getApplicationContext(), NeverStopActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(i); 
+7
source share
2 answers

You do not need Intent.FLAG_ACTIVITY_REORDER_TO_FRONT , use Intent.FLAG_ACTIVITY_SINGLE_TOP .

So this will work:

 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 

or

 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

I recommend that you read the Intent.FLAG_ACTIVITY_CLEAR_TOP documentation.


EDIT

The reason it didn't work for you is

 android:launchMode="singleInstance" 

in the manifest.
Your NeverStopActivity activity was created in a different task than others. The value of the singleInstance flag singleInstance described in Tasks and Back Stack .
I recommend you read the entire article.

+11
source

There is no need to call setFlags() and addFlags() separately, you can just call setFlags() with both the FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP :

 Intent i = new Intent(this, ActivityA.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(i); 
+3
source

All Articles