Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: EditText always remembers inputted Text

Tags:

android

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?

like image 200
hectichavana Avatar asked Sep 13 '25 19:09

hectichavana


2 Answers

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);
   }
like image 72
Thomas Avatar answered Sep 15 '25 09:09

Thomas


You want to use SharedPreferences

Roughly, it's a non-db storage of simple primitive objects that you want remembered by your application.

like image 37
josephus Avatar answered Sep 15 '25 08:09

josephus