1

有两个活动1.登陆活动和2.navigation活动,所有碎片所在......在onBackPress在这两个活动是像问题在登录和导航抽屉之间onBackpress

1.登陆活动:

@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 ModuleActivity(NavigationDrawer):

@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); 

    } 

现在当我们登录这将直接移动到活动模块,并再次注销所以我们现在在登录活性状况ty ...现在背上按下登录活动已关闭或退出成功,但当我们再次点击应用程序,它直接获取模块活动,而不是登录活动,然后再次在后退它退出活动,当我们再次启动应用程序现在它打开登录页面...不知道为什么它直接导航modulepage.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(); 

希望你能理解并在此先感谢解决

+0

所以,你的要求是,以显示loginActivity如果在用户未记录。 对不对? – Krish

+0

是的,它直接移动到第二活动.... –

+0

这是你的主要活动?我的意思是Intent.ACTION_MAIN – Krish

回答

0

谢谢你这么多@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(); 

只需完成();非常感谢的人....