简体   繁体   English

使用片段通过底部导航栏关闭应用

[英]Close app by bottom navigation bar using fragment

I am using bottom navigation bar. 我正在使用底部导航栏。 When I click on each items in bottom navigation, Fragment will replace with another one. 当我单击底部导航中的每个项目时,Fragment将替换为另一个。 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() ? 我应该使用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? 如果是,请告诉我如何?

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(); 就像您只想从BottomFragments返回,然后将所有BottomFragments放在那里(如果是),则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);
}

This can be done by sing two containers One for background fragments ie for tabs switching Second for button clicks on fragment suppose the container id is fragment_container now in your activity onBackPressed method do following 这可以通过选择两个容器来完成,一个用于背景片段,即用于选项卡切换,第二个用于片段的按钮单击,假设容器ID是fragment_container,现在在您的活动onBackPressed方法中执行以下操作

@Override public void onBackPressed() { @Override public void onBackPressed(){

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

        super.onBackPressed();
    }

1.Override activity's onBackPressed() method 1.重写活动的onBackPressed()方法

public void onBackPressed() {
    finish()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM