Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to launch Activity with FLAG_REORDER_TO_FRONT and FLAG_CLEAR_TOP

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

Activities are launched in order A->B->C->D.

Here,

  1. I want to go back to the activity A from D and resume that activity .
    So that i used the intent flag

    i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  
    
  2. Activities B,C,D instance are is no longer needed after the stmt 1.
    i go for the flag to accomplish this,

    Intent.FLAG_ACTIVITY_CLEAR_TOP 
    

In my appp using the above 1 and 2 i try to achieve like
- go back and resume the acivity A and remove the other activities from the stack
so i tried like.

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

using the above code both flags are appened here using this reference
(Android:What is difference between setFlags and addFlags for intent)

I am not able to achieve these task combined( resume the activity A and clear other).

actual calling scenario is

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 this flags to get the combined action to resume A and clear other 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 perform this action again in 1 seconds - retry / 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);
like image 225
Muthuraj Avatar asked Aug 30 '12 13:08

Muthuraj


2 Answers

You don't need Intent.FLAG_ACTIVITY_REORDER_TO_FRONT, use Intent.FLAG_ACTIVITY_SINGLE_TOP instead.

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 you to read documentation of Intent.FLAG_ACTIVITY_CLEAR_TOP.


EDIT

The reason why it wasn't working for you is

android:launchMode="singleInstance"

in manifest.
Your NeverStopActivity activity was created in different task than others. The meaning of the singleInstance flag is described in Tasks and Back Stack.
I recommend you to read the whole article.

like image 132
pawelzieba Avatar answered Nov 02 '22 15:11

pawelzieba


No need to call setFlags() and addFlags() separately, you can just call setFlags() with both FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP flags:

Intent i = new Intent(this, ActivityA.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
like image 44
Joe Avatar answered Nov 02 '22 15:11

Joe