Reputation: 1446
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?
Upvotes: 4
Views: 3159
Reputation: 76
You might want to consider using TextWatcher to save the text in the EditText whenever it's altered into SharedPreferences. Then you can pull the saved text back out of SharedPreferences in your onCreate or onResume method in your Activity.
Upvotes: 1
Reputation: 1563
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);
}
Upvotes: 2
Reputation: 919
Using Shared prefernces save the text. And while launching the application or Activity get the dat and store in the edit text field.
Upvotes: 1
Reputation: 8304
You want to use SharedPreferences
Roughly, it's a non-db storage of simple primitive objects that you want remembered by your application.
Upvotes: 1
Reputation: 9266
you could either use shared preferences if it is some user related info or probably use sqlite database to store values and retreive them on Activity load.
Cheers, Richie
Upvotes: 1