双击退出以从片段

时间:2018-05-16 06:44:15

标签: java android android-fragments

由于问题非常相似,但我没有找到确切答案。我在下面的链接上尝试了答案,但它没有用!!

Android Double Back Press to close the app having fragments

以下是我的代码:

public interface OnBackPressedListener {

void onBackPressed();
}

现在片段:

public class HomeFragment extends Fragment implements View.OnClickListener, OnBackPressedListener{

 boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
    //Checking for fragment count on backstack
    if (getFragmentManager().getBackStackEntryCount() > 0) {
        getFragmentManager().popBackStack();
    } else if (!doubleBackToExitPressedOnce) {
        this.doubleBackToExitPressedOnce = true;
        Toast.makeText(getActivity(),"Tap again to exit.", Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                doubleBackToExitPressedOnce = false;
            }
        }, 2000);
    }
    }

尝试使用    getSupportFragmentManager()也是如此,但它突出显示为红色。 我需要从片段(Android中的底部导航栏)中双击以退出应用程序。

5 个答案:

答案 0 :(得分:1)

尝试使用此代码,它始终有效。

在顶部添加此

 private boolean isFirstBackPressed = false;

现在在onBackPressed方法内添加以下代码。

添加:

@Override
public void onBackPressed() {
      if (getSupportFragmentManager().getBackStackEntryCount() != 0){
            super.onBackPressed();
      }else{
            if (isFirstBackPressed) {
                  super.onBackPressed();
             } else {
            isFirstBackPressed = true;
            showMessage("Press back again to exit");
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    isFirstBackPressed = false;
                }
            }, 1500);
        }
    }
  }    

答案 1 :(得分:1)

 private boolean isBackPressedToExit;
private void onBackPressed() {
    if (isBackPressedToExit) {
        finish();
        return;
    }
    isBackPressedToExit = true;
    Utility.toast(TabActivity.this, getString(R.string.msg_press_again_to_exit));
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            isBackPressedToExit = false;
        }
    }, 2000);
}

答案 2 :(得分:0)

您不需要处理程序:

@Override
public void onBackPressed() { 
    long currentMillis = System.currentTimeMillis();
    if (currentMillis - this.lastPressed < 2000 && getFragmentManager().getBackStackEntryCount() > 0) {
         getFragmentManager().popBackStack();
    }
    this.lastPressed = currentMillis;
}

答案 3 :(得分:0)

如果您使用的是 NavigationComponent

 @Override
    public void onBackPressed() {
        if (navController.getCurrentDestination().getId() == navController.getGraph().getStartDestination()) {
            if (doubleBackToExitPressedOnce) super.onBackPressed();
             else onHandlerStart();
            } else super.onBackPressed();
    }

    private void onHandlerStart() {
        doubleBackToExitPressedOnce = true;
        Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() doubleBackToExitPressedOnce = false;
        }, 2000);
    }

答案 4 :(得分:0)

尝试此操作将支持您的导航

@Override
public void onBackPressed()
{
    if (navController.getCurrentDestination().getId() == navController.getGraph().getStartDestination()) 
    {
        if (doubleBackToExitPressedOnce){
            super.onBackPressed();
        }
        else {
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle(R.string.app_name);
            builder.setIcon(R.drawable.feather_alert_octagon);
            builder.setMessage("Do you want to exit?")
                    .setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id)
                        {

                            finishAffinity();
                            System.exit(0);
                        }
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id)
                        {
                            dialog.cancel();
                            backFlag=0;
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();
        }
    }
    else {
        super.onBackPressed();
    }
}