2

I want to go back to the previous fragment.I tried many method like addtobackstack(null) and all. But I did not get the solution. My problem is when I click on the back button it goes to the home fragment. But I did not want it. I want that when I click on the back button it go to the previous fragment. Can anyone tell me how can I do this ?

This is my onActivityCreated() method :-

 @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            getView().setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View view, int i, KeyEvent keyEvent) {
                    if (i == KeyEvent.KEYCODE_BACK) {

                        getFragmentManager().popBackStack();
                        return true;


                    }


                    return false;
                }
            });

        }

This is the first Fragment :-

 Fragment fragment = new Packages();
 getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.container, fragment).addToBackStack(null).commit();

This is the second fragment :-

  Fragment fragment = new DeliveryFrag();
 mContext.getActivity().getSupportFragmentManager()
                            .beginTransaction()
                            .replace(R.id.container, fragment).addToBackStack(null)
                            .commit();

This is the third Fragment :-

Fragment fragment = new paytm();
   getActivity().getSupportFragmentManager().beginTransaction()
                                        .replace(R.id.container, fragment).addToBackStack(null)
                                        .commit();

I am doing the get the view and apply set onClickListner on it. but the program not enter in this method. I don't know why? Please can anyone tell me what I am doing wrong?

aarav
  • 53
  • 8
  • Possible duplicate of [how to go back to previous fragment on pressing manually back button of individual fragment?](http://stackoverflow.com/questions/14275627/how-to-go-back-to-previous-fragment-on-pressing-manually-back-button-of-individu) – Harshad Pansuriya Sep 22 '16 at 08:23
  • @Ironman i did not get the solution from that link. thatswhy i am asking again. if you know the answer please tell me. – aarav Sep 22 '16 at 08:25
  • Please tell us how are you navigating (example: Home > fragment A > fragment B > ...) – Pier Giorgio Misley Sep 22 '16 at 08:26
  • @PierGiorgioMisley i am doing navigating like Home> fragment A > fragment B > fragment C and i want to to go from fragment C to fragment B. but in my case i am going to C to Home instead of fragment B. – aarav Sep 22 '16 at 08:33
  • ok, can you please add in the answer the code you are using to go from A to B and from B to C? – Pier Giorgio Misley Sep 22 '16 at 08:34
  • @aarav try replacing the ".replace(containerId, fragment)" with ".add(conainerId, fragment)" and remove the "addToBackStack(null)" – Pier Giorgio Misley Sep 22 '16 at 09:27

4 Answers4

1

You have to save fragment in stack using fragmentManager.addToBackStack(<fragment tag>).commit();. Then try to do getFragmentManager().popBackStack();

Farman Ali Khan
  • 822
  • 7
  • 24
Ankita Shah
  • 1,866
  • 16
  • 31
0

you should call this method in onResume. like this:

 @Override
    public void onResume() {
        super.onResume();

        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_BACK) {

                    getFragmentManager().popBackStack();
                    return true;
                }

                return false;
            }
        });

    }
aarav
  • 53
  • 8
ankur
  • 51
  • 6
0

Make all public Fragment like so

public Fragment currentFrag = null;
public Fragment f1 = new Packages();
public Fragment f2 = new DeliveryFrag();
public Fragment f3 = new paytm();

Then, on the button where changing Fragment, fill your null currentFrag with the next one

@Override
public void onClick(View v){

if(currentFrag == null || currentFrag == f1){
   //open second fragment code
   currentFrag = f2;
   }else if(currentFrag == f2){
   //open third fragment code
   currentFrag = f3;
   }
}

then you override the onBackPressed() method in your main activity

@Override
public void onBackPressed()
{
  if (currentFrag == f1){
  super.onBackPressed() // close the app
  }else if(currentFrag == f2){
  //calling your first fragment code
  currentFrag = null;
  }else if(currentFrag == f3){
  //calling your second fragment code
  currentFrag = f2;
  }
}

I hope this helps because even I'm getting a bit confused

Fay Zan
  • 165
  • 2
  • 17
0

worked for me....you can try this..... may help you...

write addToBackStack(null) before commit();

and write following code in your onBackPressed() method.

  FragmentManager fm = getSupportFragmentManager();

    if (fm.getBackStackEntryCount() > 1) {
        ApplicationLog.Log(TAG,"IN > 1");
        fm.popBackStack(); //will redirect you previous visited fragment
    }
    else if(fm.getBackStackEntryCount()==1)
    {
      finish(); //will close application
    }
Mehul Gajjar
  • 431
  • 5
  • 20