android how to keep users logged in

Solutions on MaxInterview for android how to keep users logged in by the best coders in the world

showing results for - "android how to keep users logged in"
Lia
09 Nov 2020
1// siyanda zama 
2
3// you have to create a sharedpreference that will keep a value or boolean 
4// in this instance we will use an integer as follows
5//login page
6sharedpreferences = getSharedPreferences("autoLogin", Context.MODE_PRIVATE);
7        int j = sharedpreferences.getInt("key", 0);
8
9        //Default is 0 so autologin is disabled
10        if(j > 0){
11            Log.i("Loogged in","your in");
12            Intent activity = new Intent(getApplicationContext(), DashActivity.class);
13            startActivity(activity);
14        }
15        else
16        {
17            Log.i("Not luck","your not in");
18        }
19        
20// put the following code for the logout button
21 SharedPreferences sharedPreferences;
22                sharedPreferences = getActivity().getSharedPreferences("autoLogin", Context.MODE_PRIVATE);
23
24                SharedPreferences.Editor editor = sharedPreferences.edit();
25                editor.putInt("key", 0);
26                editor.apply();
27                
28// depending on whether you have a splash screen just send it to the login page
29// it will then determine whether to stay there or to go to the next page