1

Hi I have 5 fragments Now first time when I go to Activity of that fragments then default First fragment is called,then on first Fragment there is a button by clicking that button I go to the second fragment,similarly in Second fragment There is a button and clicking that button I go to the third fragment and so on.

Now My Question is that Current I am on Fifth fragment ,Now I want to go fifth fragment to second fragment,what should I do for this? Can any one please tell me?

N J
  • 27,217
  • 13
  • 76
  • 96
Rohit Sharma
  • 127
  • 1
  • 3
  • 13
  • just replace fragment and add to stack and the popup back as required – Androider Sep 01 '15 at 18:20
  • Reffer this link : http://stackoverflow.com/questions/23212162/how-to-move-from-one-fragment-to-another-fragment-on-click-of-a-imageview-in-and – Uthaya Jul 27 '16 at 09:02

5 Answers5

2

You can pop the fragment by name. While adding fragments to the back stack, just give them a name.

fragmentTransaction.addToBackStack("frag2");

Then in fragment5, pop the back stack using the name ie.. frag2 and include POP_BACK_STACK_INCLUSIVE

    someButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            FragmentManager fm = getActivity()
                    .getSupportFragmentManager();
            fm.popBackStack ("frag2", FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    });
N J
  • 27,217
  • 13
  • 76
  • 96
1

Here is the method to add and replace fragment.

 public void addReplaceFragment(Fragment fragment, int addOrReplace) {
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();

    switch (addOrReplace) {
        case addFragment:
            transaction.add(R.id.frame_containerforsearchable, fragment);
            transaction.commit();
            break;
        case replaceFragment:
            transaction.replace(R.id.frame_containerforsearchable, fragment);
            transaction.commit();
            break;
        default:
            break;
    }
}

call of fragment is..

addReplaceFragment(currencyFragmentWithSearch, replaceFragment);
  • replaceFragment integer variable
  • currencyFragmentWithSearch Fragment instance
Vinod Makode
  • 953
  • 7
  • 7
0

You can always do this :)

getActivity().getSupportFragmentManager().replace(R.id.yourFrameLayout,new Fragment2(),"FRAG2");

OR

Fragment fragment = getActivity().getSupportFragmentManager().findFragmentByTag("FRAG2");
getActivity().getSupportFragmentManager().replace(R.id.yourFrameLayout,,"FRAG2");
mudit_sen
  • 1,470
  • 15
  • 24
0

Try this code:

Fragment fragment = new SecondFragmentName();
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
AJReading
  • 1,193
  • 20
  • 35
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
0

just small correction to mudit_sen code. It is working, only if you add .beginTransaction. Thank you.

getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.FrameLayoutId,new FragmentName()).commit();
Vojtech Ruzicka
  • 16,384
  • 15
  • 63
  • 66
siva kumar
  • 35
  • 8