简体   繁体   中英

Can I use shared preferences to create login sessions in android studio when I have two types of activities to intent depending on the type of user?

I want that the user stays logged in the app until he/she logs out. I got to know that we can use shared preferences for creating such sessions. But I have two types of users and it depends on the type, which activity will intent. How can I use shared preferences in such case? And also I am using intent.putExtras(), so how would the data be passed on to other activities if the app directly opens the mainactivity class.

public void loginHandler(View view){

        userId = useridTIET.getText().toString();
        password = passwordTIET.getText().toString();
        if(userId.isEmpty()||password.isEmpty()){
            Toast.makeText(this, "Empty fields!", Toast.LENGTH_SHORT).show();
        }
        else {
            Log.i("User Type",userType);
            if(userType.equals("resident")){
                residentLogin();
            }
            else {
                guardLogin();
            }
        }
    }

public void residentLogin(){
  if(condition){
     Intent intent = new Intent(getApplicationContext(),NavigationActivity.class);
     intent.putExtra("currentResident",currentResident);
     startActivity(intent);
  }
}

public void guardLogin(){
   if(condition){
     Intent intent = new Intent(getApplicationContext(),NavigationActivity2.class);
     intent.putExtra("userId",userId);
     startActivity(intent);
   }
}

How should I proceed to create sessions? Thanks for your time:)

You can use like that

getSharedPreferences("guard",0)
getSharedPreferences("resident",0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM