[ACCEPTED]-Using shared preferences editor-sharedpreferences
Accepted answer
Edit: Of course! Those statements cannot 3 be put directly into the class at that level 2 and must be inside a method, something like 1 this:
public class QuizActivity extends Activity {
public static final String GAME_PREFERENCES = "GamePrefs";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences settings = getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefEditor.putString("UserName", "John Doe");
prefEditor.putInt("UserAge", 22);
prefEditor.putString("Gender", "Male");
prefEditor.commit();
}
}
I think you may missed up OnCreate() method 4 ,let be sure you should place the shared 3 preference in your OnCreate() method... i 2 just edited your code go through it
please 1 go through the code...below
public class A extends Activity {
static SharedPreferences settings;
public static final String PREFS_NAME = "YourPrefName";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settings = getSharedPreferences(PREFS_NAME, 0);
Log.v("UserName"," - "+settings.getString("username","android"));
SharedPreferences.Editor editor = settings.edit();
editor.putString("username","Change Android");
editor.commit();
Log.v("UserName after changed editing preference key value"," - "+settings.getString("username","android"));
}
}
SharedPreferences will work out side a onCreate() method 1 as long as it has a context:
SharedPreferences settings = getAplicationContext().getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.