8

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);
Community
  • 1
  • 1
Muthuraj
  • 447
  • 1
  • 5
  • 18

2 Answers2

14

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.

pawelzieba
  • 16,082
  • 3
  • 46
  • 72
  • this Intent.FLAG_ACTIVITY_SINGLE_TOP is similar to FLAG_ACTIVITY_REORDER_TO_FRONT or activity launch mode ="singleinstance" . We can achieve the single instace anyone of the above. My point is this clear_top is not working when add with these flags(activities on top of stack is not destroyed). how can i do this? – Muthuraj Sep 05 '12 at 05:41
  • What do you mean with "not destroyed"? Before I posted I had created sample project with activities A, B and C. Having ABC on stack and starting activity A with such intent as in the answer I get only A on stack, A wasn't recreated, and B,C were gone. Isn't is intended behavior? – pawelzieba Sep 05 '12 at 07:35
  • i also verified the same ABC . When i use the CLEAR_TOP alone and i called A from C . BC is getting destroyed, that we can see in the log which is given in onDestroy().But if we combine above things this ondestroy is not called, acitivities BC is just stopped not destroyed(i mean not clear). is that acutal behaviour or anything else? – Muthuraj Sep 05 '12 at 08:52
  • I logged all `onCreate()` `onStop()` `onResume()` `onDestroy()`. When I started new activity the old one was stopped (`onStop()`). When I started A activity from C then I get in logs such events: B - onDestroy(), A - onResume(), C - onStop(), C - onDestroy(). I can't see your code and I don't know why do you want to finish activities by yourself - system would do it for you when the activities aren't on stack or when system needs resources. Why it's important for you? – pawelzieba Sep 05 '12 at 12:14
  • this is the log i am getting in trace when i try the intent flag as you mentioned.. – Muthuraj Sep 06 '12 at 04:49
  • NeverStop(Activity1), topmenubar(activity2) D/NeverStop( 1416): ---- onPause D/TopMenuBar( 1416): ---- onStart D/TopMenuBar( 1416): ---- onResume D/NeverStop( 1416): ---- onstop D/TopMenuBar( 1416): ---- onPause D/Activity3( 1416): ---- onStart D/Activity3( 1416): ---- onResume D/TopMenuBar( 1416): ---- onstop D/Activity3( 1416): ---- onPause D/NeverStop( 1416): ---- onRestart D/NeverStop( 1416): ---- onStart D/NeverStop( 1416): ---- onResume the activity2&3 not calling onDestroy. – Muthuraj Sep 06 '12 at 04:55
  • That didn't help me. It's working for me and not for you. Could you send more details instead "it's not working for me" only? – pawelzieba Sep 06 '12 at 10:06
  • Check activity's configuration in manifest and intents which are starting those activities. Which android version and what device are you using? – pawelzieba Sep 06 '12 at 10:08
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16368/discussion-between-muthuraj-and-pawelzieba) – Muthuraj Sep 06 '12 at 13:30
  • Thanks pawelzieba ....i got the point "singleInstance"(any activities started by this one open in a separate task.) – Muthuraj Sep 07 '12 at 04:52
3

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);
Joe
  • 14,039
  • 2
  • 39
  • 49