5

I have a main activity A. There are two scenarios

1) A launches B. B has launchmode singleTask and is launched with FLAG_ACTIVITY_NEW_TASK. now I have a menu option in B which performs a delete operation and starts the activity A.

2) A launches B, which launces C it also contains the menu option to perform delet opereation.

I want A to be started with clearing the stack in both the scenarios but the activities belonging to another task still present there I am stuck is there a way to clear the stack.

wasaig
  • 666
  • 5
  • 18
  • Any information about managing the stack is located at: http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html But `FLAG_ACTIVITY_CLEAR_TOP` and `clearTaskOnLaunch` may be worth looking at, though they don't provide the exact functioning that you seem to desire. – Reed Dec 22 '11 at 10:10
  • thanks, but I have already gone through the documentation didn't find any solution. – wasaig Dec 22 '11 at 11:15
  • FLAG_ACTIVITY_CLEAR_TASK seems to be the option I am not sure though, is there any chance of using it on API Level 8. – wasaig Dec 29 '11 at 06:23
  • did u try `startActivityForResult()` in activity A and B for both the scenarios, u can also manage something using flags between these activities – Pratik Bhat Jan 01 '12 at 13:55
  • nice question +1 for you – Tofeeq Ahmad Jan 03 '12 at 08:30
  • Just to be sure this is actually working like you think it is, have you set android:taskAffinity in any of these activities? Please post your manifest, as it will help. – David Wasser Jun 20 '12 at 17:24

4 Answers4

1

try using following code on delete opereation on both B and C activity

    Intent intent=new Intent(B.this, A.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
jeet
  • 29,001
  • 6
  • 52
  • 53
  • FLAG_ACTIVITY_CLEAR_TOP doesn't work in this case because I have two different tasks running in my application. – wasaig Jan 03 '12 at 06:02
0

If i talk about your second scenario then FLAG_ACTIVITY_TOP_CLEARwill clear the stack..

And your stack now will be only A instead of A-B-C-A.

and in second case AFAIU your problem you have only two activity A and B so if you want to restart A then after restarting A manually finish B.

Hope you got some trick.

Another thing if you have activity with launchmode=SingleTask then you can use flag_activity_brought_to_front.

That will act like 

A-B-c to A

Tofeeq Ahmad
  • 11,935
  • 4
  • 61
  • 87
0

Why not finishing B and C before calling A again?

finish();
Kéza
  • 203
  • 2
  • 6
0

My method can not meet your goal A to be started with clearing the stack,But when user choose delet opereation and start B from A again the task that include A and B will be reset.

Use the flag FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET to mark the task will be clear when needed when you first start activity B from A, then if user choose delet opereation menu item from B or C, you set a flag, so next time from A(A should be single_task launch mode in manifest), you use the flag FLAG_ACTIVITY_RESET_TASK_IF_NEEDED to start B. This flag will clear B and all top of the B.

Hope this will help you :)

dreamtale
  • 2,905
  • 1
  • 26
  • 38
  • A is running in task 1 and B and C are running in task 2. Now when I start A from either B or C, I think what happening is A leaves its actual task and become part of task 2. – wasaig Jan 05 '12 at 04:45
  • You should make the launch mode of A as `singleTask` or set flag `FLAG_ACTIVITY_NEW_TASK` so that next time you start A, it will go to the existed A. – dreamtale Jan 05 '12 at 07:03
  • I have done that but when I press back button I see either B or C present on the stack. – wasaig Jan 05 '12 at 07:12
  • Try use android:launchMode="singleTask" instead of setting flag `FLAG_ACTIVITY_NEW_TASK`, It works for me. But The flag don't work for me though the android document said they are the same, i'm trying to find why. – dreamtale Jan 05 '12 at 07:19
  • B or C still in the stack though you have set the launch mode of A to singleTask in the manifest when you press back button? I'm confused. – dreamtale Jan 05 '12 at 09:58