android how to keep users logged in after the first time they log in code example

Example: android how to keep users logged in

// siyanda zama 

// you have to create a sharedpreference that will keep a value or boolean 
// in this instance we will use an integer as follows
//login page
sharedpreferences = getSharedPreferences("autoLogin", Context.MODE_PRIVATE);
        int j = sharedpreferences.getInt("key", 0);

        //Default is 0 so autologin is disabled
        if(j > 0){
            Log.i("Loogged in","your in");
            Intent activity = new Intent(getApplicationContext(), DashActivity.class);
            startActivity(activity);
        }
        else
        {
            Log.i("Not luck","your not in");
        }
        
// put the following code for the logout button
 SharedPreferences sharedPreferences;
                sharedPreferences = getActivity().getSharedPreferences("autoLogin", Context.MODE_PRIVATE);

                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putInt("key", 0);
                editor.apply();
                
// depending on whether you have a splash screen just send it to the login page
// it will then determine whether to stay there or to go to the next page

Tags:

Java Example