aarav
aarav

Reputation: 53

How to go the previous fragment on the click of the back button of phone in android?

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?

Upvotes: 2

Views: 901

Answers (4)

ankur
ankur

Reputation: 51

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;
            }
        });

    }

Upvotes: 0

Mehul Gajjar
Mehul Gajjar

Reputation: 431

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
    }

Upvotes: 0

Fay Zan
Fay Zan

Reputation: 165

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

Upvotes: 0

Ankita Shah
Ankita Shah

Reputation: 1904

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

Upvotes: 1

Related Questions