0

Navigation drawer with fragments. I have placed one fragment on main activity. But if i back press the same fragment comes again. How can I handle this?? and secondly how to implement onBackpressed in fragments?? Can anybody help me out?

in onCreate of main activity I have

    FragmentTransaction tx = getFragmentManager().beginTransaction();
    tx.replace(R.id.mainFrame, new PlaceOrderFragment());
    tx.commit();

and in back press of it I have

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}
Aditi Parikh
  • 1,522
  • 3
  • 13
  • 34

4 Answers4

0
FragmentTransaction tx = getFragmentManager().beginTransaction();
tx.replace(R.id.mainFrame, new PlaceOrderFragment()).addToBackStack("your tag");
tx.commit();

Override onBackPress() and try following code.

@Override
public void onBackPressed() {

    int count = getFragmentManager().getBackStackEntryCount();

    if (count == 0) {
        super.onBackPressed();
        //your other utility code if needed
    } else {
        getFragmentManager().popBackStack();
    }

}

Hope this will work for you.

techniqez
  • 135
  • 1
  • 9
0

You can do this using Interface. That interface will implemented in each fragment. Here is a simple example:

public interface OnBackPressedListener {
    void onBackPressed();
}

Here, if you implement this interface in your fragment then you will found this method:

@Override
    public void onBackPressed() {
        getActivity().finish(); // Or Do your specific task

    }

Now your Activity that holds that fragment , that should use onBackPressed() callback. Write bellow code in that method

@Override
    public void onBackPressed() {

        if (YOUR_DRAWER.isDrawerOpen(GravityCompat.START)) {
            YOUR_DRAWER.closeDrawer(Gravity.LEFT); //CLOSE Nav Drawer!
            return;
        }

        Fragment fragment=getYourVisibleFragment();
        if(fragment!=null){
            ((OnBackPressedListener)fragment).onBackPressed();
        }else{
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }

If you want to get Visible fragment then just use this method

public <F extends Fragment> F getVisibleFragment() {
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        if (fragments != null) {
            for (Fragment fragment : fragments) {
                if (fragment != null && fragment.isVisible()) {
                    return (F) fragment;
                }
            }
        }
        return null;
    }

That's it :)

Md. Sajedul Karim
  • 6,749
  • 3
  • 61
  • 87
0

First you need to add the fragmnet to back stack so we could retrieve it later in onBackPressed()

Replace your fragmnet transaction with this

FragmentTransaction tx = getFragmentManager().beginTransaction();
tx.replace(R.id.mainFrame, new PlaceOrderFragment());
// HERE WE WILL ADD FRAGMENT TO BACKSTACK SO WE COULD GET BACK TO IT
tx.addToBackStack(null);
tx.commit();

Replace your onBackPressed with this

@Override
public void onBackPressed() {
    // CHECKS HOW MANY FRAGMENTS ARE IN THE BACKSTACK
    int count = getFragmentManager().getBackStackEntryCount();

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        if (count == 0) {
                // IF NO FRAGMENT IN THE BACKSTACK CALL
                super.onBackPressed();
            } else {
                // GET THE LAST OPENED FRAGMENT
                getFragmentManager().popBackStack();
            }
    }
}
Atiq
  • 14,435
  • 6
  • 54
  • 69
-2

Replace ypu fragment in onCreate with fragment id , and then implement onBackpressed on the same activity and find that fragment with id and check is it visible or null and then perform operations based on it. hope you got this.

Pratik
  • 456
  • 4
  • 18