sirFunkenstine
sirFunkenstine

Reputation: 8495

Moving through multiple fragments android

Say i have fragment's A,B,C and D. The normal movement between fragments is A -> B -> C -> D. Now suppose i want to jump from A -> D, but onBackPressed() from D i want to be able to navigate back to C and then B respectively. Is there a way of doing this? The code i was attempting was something like this but it ucrrently is not working.

 public void showNestedFragment(LinkedList<Fragment> fragments, boolean allowBack)
 {

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();


    if (allowBack == false) // pop all thats in the backstack
        getSupportFragmentManager()
                .popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

    for(Fragment fragment : fragments){
         fragmentTransaction.add(R.id.fragment, fragment);

          if (allowBack)
          {
               fragmentTransaction.addToBackStack(null);
          }

           try
           {
               fragmentTransaction.commit();
           }
          catch (IllegalStateException e)
           {
               e.printStackTrace();
           } 
    }
 }

Upvotes: 1

Views: 101

Answers (1)

Jatin Malwal
Jatin Malwal

Reputation: 5263

You have fragments so you can do anything you want. You should follow these steps to achieve this.

  • First declare all fragments in manifest in order A than B than C than D
  • So you can achieve normal navigation from A>B>C>D using simple approach of set visibility on of next fragment.
  • When you want direct navigation from A to D simply hide B and C fragment.
  • When you want to move from D>C>B>A simply visible B and C again and follow the simple navigation again.

Upvotes: 1

Related Questions