tags:

views:

505

answers:

4

I defined an activity ExampleActivity.

When my application was launched, an instance of this activity was created, say it is A. When user clicked a button in A, another instance of ExampleActivity, B was created. Now the task stack is B, A, with B at the top. Then, user clicked a button on B, another instance of ExampleActivity, and C was created. Now the task stack is C, B, A, with C at the top.

Now, when user click a button on C, I want the application to bring A to the foreground, i.e. make A to be at the top of task stack, A, C, B.

How can I write the code to make it happen?

Thanks.

+1  A: 

I think a combination of Intent flags should do the trick. In particular, Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK.

Add these flags to your intent before calling startActvity.

See http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

Al
+1  A: 

In general I think this method of activity management is not recommended. The problem with reactivating an activity two Steps down in The Stack is that this activity has likely been killed. My advice into remember the state of your activities and launch them with startActivity (). I'm Sure you've Seen this page but for your convenience this link: http://developer.android.com/reference/android/app/Activity.html

Segfault
+2  A: 

You can try this FLAG_ACTIVITY_REORDER_TO_FRONT (the document describes exactly what you want to) http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT

Bino
A: 

The best way I found to do this was to use the same intent as the Android home screen uses - the app Launcher.

For example:

Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(this, MyMainActivity.class);
i.setComponent(cn);
startActivity(i);

This way, whatever activity in my package was most recently used by the user is brought back to the front again. I found this useful in using my service's PendingIntent to get the user back to my app.

-g

greg7gkb