Thursday, 30 August 2018

How To Store User ID using SharedPreferences

Step 1:

Set Method 

int user_id=bean.getId();

store(user_id);


private void store(int userID){
    SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
    editor.putInt("User_id", userID);
    editor.commit();
}



Get Method 

private int getUserID(){
    SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
    int user_id = prefs.getInt("User_id", 0);
    return user_id;
}

int id=getUserID();
if(id == 0){
    Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
    startActivity(intent);
    finish();
}else{
    Intent i=new Intent(SplashActivity.this,MainActivity.class);
    startActivity(i);
    finish();
}


No comments:

Post a Comment

HOW TO CREATE CIRCLE IMAGE VIEW

Step 1: implementation 'de.hdodenhof:circleimageview:3.0.0' Step 2: <de.hdodenhof.circleimageview.CircleImageView xmlns:ap...