1

I am using bottom navigation bar. When I click on each items in bottom navigation, Fragment will replace with another one. when I want to press back I want to exit from application. I think this is easy.

But the problem is here: In every Fragment I have button. if we click on button it will replace with another fragment. if I press back button, I want to go to previous fragment that it was in bottom navigation bar. after this if user pressed back again, the app should exit. what should I do?

Should I use onBackPressed()?

I used this code BUT not working

boolean pressBackForExit = false;
@Override
public void onBackPressed() {
    if (getSupportFragmentManager().getBackStackEntryCount() == 0){
        pressBackForExit = true;
    }
    if (pressBackForExit){
        finish();
    }
    if (getSupportFragmentManager().getBackStackEntryCount() > 0){
        getSupportFragmentManager().popBackStack();
        if (getSupportFragmentManager().getBackStackEntryCount() == 0){
            pressBackForExit = true;
        }
    }
}

If yes, tell me How?

pankajagarwal
  • 13,462
  • 14
  • 54
  • 65
hosein2rd
  • 45
  • 10
  • Please Refer the following link : https://stackoverflow.com/questions/14275627/how-to-go-back-to-previous-fragment-on-pressing-manually-back-button-of-individu/14275833 – Arya Feb 06 '18 at 09:38
  • It's OK. but sometime if user press back it should come back to previous fragment – hosein2rd Feb 06 '18 at 10:07

3 Answers3

1

First you have to get the fragment in which you are now. Like you want back from only BottomFragments then put all BottomFragments there if yes, then onBackPress();

   Fragment f = getActivity().getFragmentManager().findFragmentById(R.id.fragment_container);
      if (f instanceof NavBottomOneFragment ) {
            onBackPressed();
       }

Try with below code, Put this code on your Activity Class. This is the code for double press to exit the app.

boolean doubleBackToExitPressedOnce = false;

 @Override
public void onBackPressed() {
    if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }
    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Press again to exit..", Toast.LENGTH_SHORT).show();
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            doubleBackToExitPressedOnce=false;
        }
    }, 2000);
}
Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44
  • It will finish my activity.when user press back, if the fragment was one of items in bottom navigation bar should finish the activity. if the fragment wan't one of items in bottom navigation should come back to previous fragment. got it? – hosein2rd Feb 06 '18 at 10:10
  • @hosein i updated code just try , sure it will work for you – Abhishek kumar Feb 06 '18 at 10:43
  • As i have not seen your full code replace "R.id.fragment_container)" with your FrameLayout container inside your activity.xml – Abhishek kumar Feb 07 '18 at 06:26
  • What i'm doing here it will get your bottomMenu fragment , when it will get bottomMenu fragments then it will call to exit. @hosein, Try to implement it and let me know. – Abhishek kumar Feb 07 '18 at 06:28
  • I used "f (f instanceof NavBottomOneFragment )" in my onBackPressed() it worked. thanks for your help ;) – hosein2rd Feb 07 '18 at 06:58
  • Welcome, Happy to help you @hosein – Abhishek kumar Feb 07 '18 at 06:59
0

This can be done by sing two containers One for background fragments i.e. for tabs switching Second for button clicks on fragment suppose the container id is fragment_container now in your activity onBackPressed method do following

@Override public void onBackPressed() {

 Fragment frag = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
 if (frag != null){ getSupportFragmentManager().beginTransaction().remove(frag).commit();
return;                
}

        super.onBackPressed();
    }
Satender Kumar
  • 627
  • 4
  • 7
  • this code will remove All Fragmets. I need the Fragments in bottom navigation bar. I don't want to remove theme. – hosein2rd Feb 06 '18 at 09:59
0

1.Override activity's onBackPressed() method

public void onBackPressed() {
    finish()
}
leimenghao
  • 226
  • 1
  • 10
  • It will finish my activity.when user press back, if the fragment was one of items in bottom navigation bar should finish the activity. if the fragment wan't one of items in bottom navigation should come back to previous fragment. got it? – hosein2rd Feb 06 '18 at 10:03
  • you can use `Transition.addToBackStack()` when you show fragment not of items in bottom navigation. Then you can get what you want. More details see [What is the meaning of addToBackStack with null parameter?](https://stackoverflow.com/questions/22984950/what-is-the-meaning-of-addtobackstack-with-null-parameter) – leimenghao Feb 06 '18 at 10:23
  • And may be you can use `Transition.add()` instead of `Transition.replace()` when to show other fragment which is not of items in bottom navigation. – leimenghao Feb 06 '18 at 10:33