OnBackPressed()退出应用程序

时间:2016-11-22 12:08:59

标签: android android-fragments

我的onBackPressed()将我从我的应用中删除了。我怎么能避免这个?要么它应该将我发送回最后显示的片段,要么只是发送给我一个特定的片段。

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    }
    else{
        super.onBackPressed();
    }
}

4 个答案:

答案 0 :(得分:1)

试试这个,如果您使用的是Fragment android.support.v4.app.Fragment。

 private boolean allowExit = false;

然后

  @Override
public void onBackPressed() {
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
         drawer.closeDrawer(GravityCompat.START);
     }
   else if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
        // handling fragment backbutton navigation
        getSupportFragmentManager().popBackStack();
    } else {
        if (allowExit) {

            super.onBackPressed();
        } else {
            Toast.makeText(this, getString(R.string.app_exit_message), Toast.LENGTH_SHORT).show();
            allowExit = true;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    allowExit = false;
                }
            }, 3000);
        }
    }
}

如果您使用的不是支持片段,则必须替换

  @Override
public void onBackPressed() {
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
         drawer.closeDrawer(GravityCompat.START);
     }
   else if (getFragmentManager().getBackStackEntryCount() > 0) {
        // handling fragment backbutton navigation
        getFragmentManager().popBackStack();
    } else {
        if (allowExit) {

            super.onBackPressed();
        } else {
            Toast.makeText(this, getString(R.string.app_exit_message), Toast.LENGTH_SHORT).show();
            allowExit = true;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    allowExit = false;
                }
            }, 3000);
        }
    }
}

并将消息添加到string.xml

<string name="app_exit_message">Press again to exit</string>

答案 1 :(得分:0)

请尝试以下代码。
1.如果您的抽屉打开,它将关闭抽屉 2.如果任何片段在backstack中,它将导航到前一个片段 3.如果No Fragment在Backstack中,它将关闭应用程序。

        @Override
        public void onBackPressed() {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                oneStepBack();
            }
        }

        /**
         * function to go back to previous fragment
         */
        private void oneStepBack() {
            FragmentTransaction fts = getSupportFragmentManager().beginTransaction();
            FragmentManager fragmentManager = getSupportFragmentManager();
            showOutput("Back Stack Count>>>>>>>>>>" + fragmentManager.getBackStackEntryCount());
            if (fragmentManager.getBackStackEntryCount() >= 2) {
                fragmentManager.popBackStackImmediate();
                fts.commit();
            } else {
                finish();
            }
        }

答案 2 :(得分:0)

public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    }
    else{

        write your code here what activity you want to open on back press   and remove below line 
        //super.onBackPressed();
    }
}

答案 3 :(得分:0)

应该在onCreate()方法中初始化DrawerLayout,因此您将遇到问题。

供您参考检查,它可能对您有所帮助。 http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/