0

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

I write below code for open this Drawer :

reviewSerialFrag_DrawerLayout.openDrawer(Gravity.END);

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

reviewSerialFrag_DrawerLayout.closeDrawer(Gravity.END);

How can I it? Please help me

5 Answers5

0
/Try to use below code snipet/

  @Override
  public void onBackPressed()
  {

int count = getFragmentManager().getBackStackEntryCount();

// count --> Is your current fragment 

if (count == 0) 
{
    if(reviewSerialFrag_DrawerLayout.isDrawerVisible(GravityCompat.END))
    {
        reviewSerialFrag_DrawerLayout.closeDrawer(GravityCompat.END);
    }

} 


}
yash786
  • 1,151
  • 8
  • 18
  • thanks, but we can't use `public void onBackPressed()` methos in fragments my bro –  Sep 12 '17 at 14:07
  • I can't use @Override public void onBackPressed() . :( –  Sep 12 '17 at 14:12
  • @YoYoAnimate no need to use onBackPressed in Fragment use it in Activity and check the fragment count.If count is your Navigation Drawer Fragment then check the Navigation Drawer is open or not. If open the closed it.. – yash786 Sep 12 '17 at 14:14
0

In your Activity onBackPressed() write below code

@Overrdie
public void onBackPressed(){
Fragment currentFragment = getActivity().getFragmentManager().findFragmentById(R.id.fragment_container);

   if(currentFragment instanceof YourDrawerFragment && reviewSerialFrag_DrawerLayout.isDrawerVisible(GravityCompat.END))
        {
            reviewSerialFrag_DrawerLayout.closeDrawer(GravityCompat.END);
            return;
        }
    super.onBackPresses():
}
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
0

You can do this to close your drawer on back press

@Overrdie
public void onBackPressed(){
super.onBackPresses():
if(reviewSerialFrag_DrawerLayout.isDrawerVisible(GravityCompat.END)){
        reviewSerialFrag_DrawerLayout.closeDrawer(GravityCompat.END);
 }
}
Shriyansh Gautam
  • 1,084
  • 1
  • 7
  • 13
0

In general, I find it easiest to use the Observer pattern and delegate the back pressed event down to the fragment. This will allow you to keep the activity and fragment concerns separated.

interface OnBackPressedListener {
  public void onBackPressed();
}

Then, in your fragment, implement this OnBackPressedListener

class MyFragment extends Fragment implements OnBackPressedListener{

  public void onBackPressed(){...}

}

And finally, in your activity, you can do the following:

class MyActivity extends Activity {
  @Override
  public void onBackPressed(){

    // Grab all the fragments that are 'observing' the back press event
    Fragment currentFragment =
      getFragmentManager().findFragmentById(R.id.fragment_container);

    if(currentFragment != null && currentFragment instanceof OnBackPressedListener) {
      // delegate this back press event down to the fragment
      OnBackPressedListener backFragment = (OnBackPressedListener) currentFragment;
      backFragment.onBackPressed();
    }

    super.onBackPressed():
}
Jon
  • 1,715
  • 12
  • 14
  • thanks, but not found `R.id.fragment_container` . what is `R.id.fragment_container` ? –  Sep 12 '17 at 18:57
  • That is the id you for the container hosting your fragment. This was just an example so your id is probably different. – Jon Sep 13 '17 at 01:57
  • @Hey my friend, can you send me your Skype ID? please –  Sep 13 '17 at 03:46
  • Sorry, I don't use Skype. If you have a specific question you should ask it here. If we can find the answer, I'm sure our discussion will help other people in the future. – Jon Sep 13 '17 at 22:08
0

In Jon's solution, the super.onBackPressed will always be called. This may not necessarily be the case. To do this, it is enough that the implementation returns a boolean and act according to the result.

My solution almost identical here

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

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