byteC0de
byteC0de

Reputation: 5273

Android back button not working in fragment

I am new to android, I am called fragment_1 from my MianActivity then I am called fragment_2 from fragment_1. That is working fine. When I Press back button from fragment_2 it's not working (I want to go back to fragment_1)

   Fragment someFragment = new Fragment2();
   FragmentTransaction transaction = getFragmentManager().beginTransaction();
   transaction.replace(R.id.relative_layout_to_replace, someFragment );
   transaction.addToBackStack(null);
   transaction.commit();

Upvotes: 3

Views: 11820

Answers (6)

Saurav Jha
Saurav Jha

Reputation: 1

  1. It's very simple just add null after the navigation fragment and after that add .addToBackStack(null).commit()

  2. Main thing is you have to add everywhere the same thing. (R.id.name,fragment(),null).addToBackStack(null).commit()

requireFragmentManager().beginTransaction()
                .replace(R.id.fragmentContainerView, SingUpFragment(),null).addToBackStack(null).commit()

Upvotes: 0

byteC0de
byteC0de

Reputation: 5273

This code worked for me

@Override
public void onBackPressed(){
  if (getSupportFragmentManager().getBackStackEntryCount() == 1){
    finish();
  }
  else {
    super.onBackPressed();
  }
}

Upvotes: 3

Reejesh PK
Reejesh PK

Reputation: 4130

If you are using toolbar back buttons, then, make sure you have this code in your fragment:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //required for back button to work
    setHasOptionsMenu(true);
}

And also override this function:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.i(TAG,"Back Button Pressed");
        switch (item.getItemId()) {
            case android.R.id.home:
                Log.i(TAG,"home on backpressed");
                getActivity().onBackPressed();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

Upvotes: 5

Bhavnik
Bhavnik

Reputation: 2048

Note:- Always add first fragment in transaction and then replace every fragment in transaction, in order to go back to previous fragment you have to add that fragment to backstack as below

FragmentTransaction fragTrans = fragmentManager.beginTransaction();
fragTrans.addToBackStack("DashboardFragment");
fragTrans.replace(contentFrameId, openDashBoard, "OpenDashboardFragment");

onBackPressed in MainActivity

if (fragmentManager.getBackStackEntryCount() == 1) {
      AlertExit();
} else {
      int backStackId = fragmentManager.getBackStackEntryAt(i).getId();
      fragmentManager.popBackStack(backStackId, FragmentManager.POP_BACK_STACK_INCLUSIVE);

Upvotes: 0

Rajesh Gosemath
Rajesh Gosemath

Reputation: 1872

From Main Activity:

getSupportFragmentManager().beginTransaction()
   .add(R.id.something,new Fragment1()).addToBackStack(null).commit();

From fragment1:

getActivity().getSupportFragmentManager().beginTransaction()
   .replace(R.id.something,new Fragment2()).addToBackStack(null).commit();

Upvotes: 0

Sean Goudarzi
Sean Goudarzi

Reputation: 1544

Your fragment has already been added to backstack. But you need to 'Pop The Backstack' once, to make the Android Device show the fragment you added to its backstack. Simply add these lines of code in your Activity's onBackPressed()

FragmentManager manager = getSupportFragmentManager();
    if(manager.getBackStackEntryCount() > 0 ) {
        manager.popBackStack();//Pops one of the added fragments
    }

Upvotes: 0

Related Questions