I have an EditText field and a check box inside an activity. What I want is, whenever the checkbox is checked, the inputted text inside the EditText field will be saved and everytime the user opens the app, the text he/she enters the last time are still there.
How am I able to perform that?
Call and commit to the SharedPreferences in the OnStop(), and call it again in onCreate. Something like this:
private void SavePreferences(String key, String value){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
private void LoadPreferences(){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String strSavedMem1 = sharedPreferences.getString("MEM1", "");
String strSavedMem2 = sharedPreferences.getString("MEM2", "");
textSavedMem1.setText(strSavedMem1);
textSavedMem2.setText(strSavedMem2);
}
You want to use SharedPreferences
Roughly, it's a non-db storage of simple primitive objects that you want remembered by your application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With