Search code examples
androidauthenticationnavigation-draweronbackpressed

Issue in onBackpress between Login and Navigation drawer


there are two activity 1.Login activity and 2.navigation activity where all fragments lies... the onBackPress in both activities is like

1.Login Activity:

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

}

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

    }

Now when we login it will directly move to activity module and again logout so now we are at login activity...now on back press the login activity is closed or exit successfully but when again we click on app it directly obtain on module activity rather then login activity then again on backpress it quits the activity and when we start again app now it opens the login page...dont know why it directly navigates on modulepage.Worst error or we can say small yet big error.

this is how I manage session in login:

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

and in logout:

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

Hope you understand and thanks in advance for solving


Solution

  • Thank you so much @krish for solving my Question its bit easy on 2nd Activity onBackPressed will be as

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

    and on logout:

       /*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();
    

    Simply finish(); thanks alot man....