I am running into an issue with Shared preferences. It isn’t working as I expected. Upon login of my application I store an id (google id) in the shared preferences. On a subsequent activity (about 3 activities later) I attempt to retrieve the id but it is null. In case it is important, I am attempting to retreive the shared preferences in a onClickListener (from a button). I am not sure what I am doing wrong. Any help would be appreciated.
How I am setting it:
public void setLoginPreferences(String id){
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString(Constants.ID_KEY_NAME, id);
editor.commit();
}
How I am attempting to get it:
//setup clickListener for Sumbit Comment
Button submitButton = (Button) findViewById(R.id.submitComment);
submitButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//myContext is a global variable set on the onCreate of the activity
SharedPreferences prefs = myContext.getPreferences(MODE_PRIVATE);
String userId = prefs.getString(Constants.ID_KEY_NAME, null);
SetConcertCommentAsynchWebservice scca = new SetConcertCommentAsynchWebservice(myContext,concertId,userId,Float.toString(soundRatingBar.getRating()),Float.toString(showRatingBar.getRating()),userSubmittedComments.getText().toString());
}});
}
you may want to use
context.getSharedPreferences()instead ofcontext.getPreferences(), if you want to access them somewhere else.