If I have various activities - A, B, C, D, E, F.
A opens B, B opens C, C opens D and so on like this A->B->C->D->E->F.
Currently I am at F activity after opening all the activities in backstack.
Now I want to go to B activity from F without closing A but closing all the activities(C, D, E, F).Please tell how to do this?
Asked
Active
Viewed 67 times
0

Sahil Munjal
- 463
- 2
- 15
-
use finish() method to destroy classes that you don' want to put in backstack. – Kunu Dec 16 '16 at 10:16
-
Use `intent.setFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP);` – Piyush Dec 16 '16 at 10:16
-
see this [enter link description here](http://stackoverflow.com/questions/5794506/android-clear-the-back-stack) – Dec 16 '16 at 10:19
-
It's super easier with Fragment . – QuokMoon Dec 16 '16 at 10:22
-
@Piyush This helped, thanks bro!! – Sahil Munjal Dec 16 '16 at 10:30
-
when you don't require your activity to be in the backstack,just call finish for that activity that you don't want to be opened again.Use finish() after starting activity C>D>E. – Nithin Dec 16 '16 at 10:32
3 Answers
1
Try this one.
- Activity A calls B (Finish A activity)
- Activity B calls C (Finish B activity) Call A Activity using onBackPressed method in B activity
- Activity C calls D (Finish C activity) Call C Activity using onBackPressed method in D activity
- Activity D calls E (Finish D activity) Call D Activity using onBackPressed method in E activity
- Activity E calls F (Finish E activity) Call B Activity using onBackPressed method in F activity

Bhavin Shah
- 361
- 4
- 11
-
-
Just a simple modification required as per user requirement. Don't finish class A and B as he wants them to stay in stack. – Kunu Dec 16 '16 at 10:33
0
In this case you'll have to use Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent intent - new Intent(this, B.class);
intent.setFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
From the documentation:
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.

Ertivinme
- 36
- 3
0
You should use a MainActivity and multiple Fragments for these kinds of operations.
First, create a Fragment container (basically, a view that can hold a Fragment, you can change that Fragment anytime from your MainActivity).
Your XML layout for the MainActivity:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Then change the fragment in the container, in your MainActivity like this:
// Create a new Fragment to be placed in the activity layout
YourFragment firstFragment = new YourFragment();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();

MaroonBeret
- 3
- 3