Android Double Back按以关闭包含片段的应用程序 [英] Android Double Back Press to close the app having fragments

查看:76
本文介绍了Android Double Back按以关闭包含片段的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了本教程,并遵循了一些类似的答案.

I followed this tutorial and certain similar answers on SO.

我现在的 onBackPressed 代码如下-

private static final int TIME_DELAY = 2000;
private static long back_pressed;

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        int fragments = getFragmentManager().getBackStackEntryCount();
        if (fragments > 0) {
            super.onBackPressed();
        } else {
            if (back_pressed + TIME_DELAY > System.currentTimeMillis()) {
                super.onBackPressed();
            } else {
                Toast.makeText(getBaseContext(), "Press once again to exit!",
                        Toast.LENGTH_SHORT).show();
            }
            back_pressed = System.currentTimeMillis();
        }
    }
}

我正在像这样将片段添加到后堆栈(在某些地方,我不添加到后堆栈)-

I am adding fragments to back stack like this (and at some places I don't add to back stack) -

private void LoadSignDetailsFragment() {
    Bundle args = new Bundle();
    Fragment fragment = new SignDetailsFragment();
    args.putBoolean("hasValues", true);
    args.putBoolean("showBookmarkedSignsOnly", showBookmarkedSignsOnly);
    args.putInt("sign_id", signId);
    if (fragment != null) {
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        fragment.setArguments(args);
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);
        fragmentTransaction.replace(R.id.container_body, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
}

我想做的是,如果后堆栈中有任何片段,则将单个 onBackPressed 迁移到先前的片段.但是,如果堆栈中没有片段,则应显示Toast以便按两次后退键以关闭应用程序.

What I am trying to do is, if there is any fragment in backstack, single onBackPressed migrate to previous fragment. But, if there no Fragment in backstack, it should display Toast for double back press to close the app.

我当前的代码,总是显示Toast,并要求进行两次反向按下,而不管是否在堆栈中有碎片.我不知道为什么吗?

My present code, always shows the Toast, and asks for Double back press irrespective of presence/absence of fragments in backstack. I am unable to figure out why?

推荐答案

您可以根据需要参考以下代码.如果未使用v4支持片段,则必须使用 getFragmentManager()而不是 getSupportFragmentManager()来获取后退堆栈计数.在这里,我使用布尔值来检查是否单击了back,如果在2秒钟内没有再次单击它,它将再次变为false.

You can refer to below code for your need. If you are not using v4 support fragment, then you have to use getFragmentManager() instead of getSupportFragmentManager() to get the backstack count. Here I am using boolean value to check if back is clicked, if in 2 seconds it is not clicked again, it will become false again.

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

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

            @Override
            public void run() {
                doubleBackToExitPressedOnce = false;
            }
        }, 2000);
    } else {
        super.onBackPressed();
        return;
    }
}

这篇关于Android Double Back按以关闭包含片段的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆