0

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?

Sahil Munjal
  • 463
  • 2
  • 15

3 Answers3

1

Try this one.

  1. Activity A calls B (Finish A activity)
  2. Activity B calls C (Finish B activity) Call A Activity using onBackPressed method in B activity
  3. Activity C calls D (Finish C activity) Call C Activity using onBackPressed method in D activity
  4. Activity D calls E (Finish D activity) Call D Activity using onBackPressed method in E activity
  5. Activity E calls F (Finish E activity) Call B Activity using onBackPressed method in F activity
Bhavin Shah
  • 361
  • 4
  • 11
  • This will close all the class and start instance of new B activity. – Kunu Dec 16 '16 at 10:20
  • 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();