0

In my application I use ViewPager for show two fragments in an activity. In one of the fragments I use NavigationDrawer. I want when click on onBackPress close this NavigationDrawer.

I wrote below code for open this Drawer :

reviewSerialFrag_DrawerLayout.openDrawer(Gravity.END);

I want that when I click on onBackPress it will close this drawer with below code:

reviewSerialFrag_DrawerLayout.closeDrawer(Gravity.END);

questioner, put in what the current problem is, please

Al Lelopath
  • 6,448
  • 13
  • 82
  • 139

5 Answers5

1

onBackpress() only called in fragment if you need back press event in fragment you have to implement interface to get onBackPress() callback.

In Activity:

public MyActivity extends AppCompatActivity{

private BackPressListener backPressListener;

    @Override
    public void onBackPressed() {
        if (backPressListener != null) {
            backPressListener.onActivityBackPress();
        } else {
            super.onBackPressed();
        }
    }

    public void setBackPressListener(BackPressListener backPressListener) {
        this.backPressListener = backPressListener;
    }

    public interface BackPressListener{
        void onActivityBackPress();
    }

}

In Fragment:

public class MyFragment extends Fragment implements BackPressListener{

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((MyActivity)getActivity()).setBackPressListener(this);
    }

    @Override
    public void onActivityBackPress() {
        // handle your back press here.

       reviewSerialFrag_DrawerLayout.closeDrawer(Gravity.END);

       getActivity().onBackPressed();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ((MyActivity)getActivity()).setBackPressListener(null);
    }
}
Bhuvanesh BS
  • 13,474
  • 12
  • 40
  • 66
1

My solution:
Create BaseFragment.java:

public class BaseFragment extends Fragment
    {
        public boolean onBackPressed()
        {
             return false;
        }
    }

then extend your fragment by this BaseFragment, and in activity:

@Override
public void onBackPressed()
{
    if (!yourFragment.onBackPressed())
        super.onBackPressed();
}

In yourFragment:

public class YourFragment extends BaseFragmnet
{

    ...
    .
    .

    @Override
    public boolean onBackPressed()
    {
        // do something...

        return true; // you should return true;
    }
}
1

According to the AndroidX release notes, androidx.activity 1.0.0-alpha01 is released and introduces ComponentActivity, a new base class of the existing FragmentActivity and AppCompatActivity. And this release brings us a new feature:

You can now register an OnBackPressedCallback via addOnBackPressedCallback to receive onBackPressed() callbacks without needing to override the method in your activity.

TonnyL
  • 1,226
  • 1
  • 12
  • 17
0

The best solution is create your interface, and implement in Fragment.

Solution here : implement onBackpress in Fragment

public interface IOnBackPressed {
    /**
     * Si vous retouné true le back press ne sera pas pris en compte, sinon l'activité agira naturellement
     * @return true si votre traitement est prioritaire sinon false
     */
    boolean onBackPressed();
}

see link for more details ... easy sample

Maxime Jallu
  • 2,352
  • 1
  • 10
  • 12
0

You can keep the flag that drawer was opened. And when you override the onBackPressed(), check this flag and if it's true, call

reviewSerialFrag_DrawerLayout.closeDrawer(Gravity.END);

otherwise, call super.onBackPressed() or any other logic you need.

Dmitriy Mitiai
  • 1,112
  • 12
  • 15