在登录和导航抽屉之间的Onbackpress中发出问题



有两个活动1. login活动和2.逐步活动,所有片段都位于...两种活动中的onbackpress都像

1.Login活动:

@Override
public void onBackPressed() {
    Intent a = new Intent(Intent.ACTION_MAIN);
    a.addCategory(Intent.CATEGORY_HOME);
    a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(a);
    super.onBackPressed();
}

和2。

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
        try {
            FragmentManager fragmentManager = getSupportFragmentManager();
            Fragment fragment = fragmentManager.findFragmentByTag("Home");
            if (fragment != null) {
                if (fragment.isVisible()) {
                    this.exit = true;
                    Toast.makeText(this, "Press Back again to Exit", Toast.LENGTH_SHORT).show();
                    Intent a = new Intent(Intent.ACTION_MAIN);
                    a.addCategory(Intent.CATEGORY_HOME);
                    a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(a);
                }
            } else {
                fragment = Frag_home.class.newInstance();
                getFragmentManager().popBackStack();
                fragmentManager.beginTransaction().replace(R.id.content_frame, fragment, "Home").commit();
                displaySelectedScreen(R.id.nav_home);
            }
        } catch (Exception e) {
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                exit = false;
            }
        }, 2000);
    }

现在,当我们登录时,它将直接移动到活动模块并再次注销模块活动而不是登录活动,然后再次在背压上退出活动,当我们再次启动应用程序时,现在它将打开登录页面...不知道为什么它直接在模块上导航。Worst错误,或者我们可以说小但很大的错误。

这是我在登录中管理会话的方式:

if (login.getInt("session", 0) == 1) {
        Intent i = new Intent(Login.this, ModulesActivity.class);
        startActivity(i);
        Log.e("hello", "hello" );
        finish();
    } else {
        checkpermission();
        main();
    }
 }

和注销:

  final SharedPreferences login = getSharedPreferences(
                                        "Login", ModulesActivity.MODE_PRIVATE);
                                SharedPreferences.Editor editor = login.edit();
                                editor.putInt("session", 0);
                                editor.commit();

希望您理解并提前感谢您解决

非常感谢@krish解决我的问题,在第二个活动onbackpressed 上很容易,将为

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

        int count = getSupportFragmentManager().getBackStackEntryCount();
        Log.v("Your activty", "count " + count);
        Log.v("Your activty", getSupportFragmentManager().findFragmentById(R.id.content_frame) + "");
        if (getSupportFragmentManager().findFragmentById(R.id.content_frame) instanceof Frag_home) {
            System.exit(0);
        }
        super.onBackPressed();
    }
}

和注销:

   /*Session*/
                                final SharedPreferences login = getSharedPreferences(
                                        "Login", ModulesActivity.MODE_PRIVATE);
                                SharedPreferences.Editor editor = login.edit();
                                editor.putInt("session", 0);
                                editor.commit();
                                Intent intent = new Intent(ModulesActivity.this, Login.class);
                                startActivity(intent);
                                finish();

简单完成((;谢谢很多人....

相关内容

  • 没有找到相关文章

最新更新