Daniele Vitali
Daniele Vitali

Reputation: 3858

Resume activity closing the top ones on stack

Suppose to have 3 activities: A, B, C. I launch A than B than C. How can I resume activity A (NOT create it again) closing B and C?

EDIT:

Start B from A:

Intent intent = new Intent(this, B.class);
startActivity(intent);

Android calls onPause and onStop for A.

Start C from B:

Intent intent = new Intent(this, C.class);
startActivity(intent);

Resume A from C:

Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Android calls onDestroy(), onCreate() and onResume() for A.

Upvotes: 0

Views: 312

Answers (2)

Daniele Vitali
Daniele Vitali

Reputation: 3858

Found the solution by myself.

Combining Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_SINGLE_TOP Android does not destroy activity A.

Upvotes: 1

Jitender Dev
Jitender Dev

Reputation: 6925

Use FLAG_ACTIVITY_CLEAR_TOP flag for starting intent.

Intent intent_to_a=new Intent(C.this,A.class);
                intent_to_home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent_to_a);

Your activity A will be resumed only if it is not destroyed manually using finish(); or by the OS itself.

From Android Docs

public static final int FLAG_ACTIVITY_CLEAR_TOP

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

Upvotes: 1

Related Questions