hectichavana
hectichavana

Reputation: 1446

Android: EditText always remembers inputted Text

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

Answers (5)

dr4g0n
dr4g0n

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

Thomas
Thomas

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

user493244
user493244

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

josephus
josephus

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

Richie
Richie

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

Related Questions