双击以退出底部导航菜单项上的应用程序

Double click to exit app on bottom navigation menu item

每个帖子都是针对导航抽屉的,但是由于我使用的是底部导航,所以我找不到任何解决方案。搜索并尝试了所有线程。

这是我选择菜单项的选择器方法

public boolean onNavigationItemSelected(@NonNull MenuItem item) 

          {

            View v=null;

            int id = item.getItemId();

            switch (id){

                case R.id.search:
                    fragment = new Search();

                    break;
                case R.id.todo:
                    fragment = new ServiceTable();
                    break;
                case R.id.info:
                    fragment = new Orderlist();
                    break;
                case R.id.close:

                    //have to implement double click here.

                    break;

            }

            final FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.main_container, fragment).commit();
            return true;
        }


    });

    if (savedInstanceState == null) {
        bottomNavigation.setSelectedItemId(R.id.search);
    }
}

你可以写

boolean isClickedTwice = false;
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        View v = null;
        int id = item.getItemId();
        switch (id){
            case R.id.close:

                //have to implement double click here.
                if (isClickedTwice) {
                  this.finish();
                }
                isClickedTwice = true;

                break;

        }

        final FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.main_container, fragment).commit();
        return true;
    }


});

我看到你在使用片段。您是否为此维护任何返回堆栈跟踪?我建议你这样做,然后你实现双击退出应用程序。让我知道

allowedToExit 是一个布尔值,用于跟踪用户是否允许退出。我们根据堆叠片段的数量以及用户是否按下两次来判断。我在我的 onBackPressed 方法中使用了这个逻辑来创建一个两次返回按退出应用程序

//fragments remove logic
        int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount();
        // this is last item
        if (backStackEntryCount == 1) {
            if (allowedToExit)
                finish();
            else {
                allowedToExit = true;
                Util.showToast(BaseActivity.this,
                        "Press again to exit", false);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        allowedToExit = false;
                    }
                }, 1000);
                return;
            }
        }
        // we have more than 1 fragments in back stack
        if (backStackEntryCount > 1) {
            Util.hideSoftKeyboard(BaseActivity.this);
            onRemoveCurrentFragment();
        } else
            super.onBackPressed();

您必须创建一个全局布尔变量来检查双击功能,例如:

boolean doubleBackToExitPressedOnce = false;

在此之后,在您的 case R.id.close

中执行以下代码
if (doubleBackToExitPressedOnce) {
            finishAffinity();
        }
        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);