Kush Grover
Kush Grover

Reputation: 373

How to retain EditText value on reopening of app?

I am creating an android application which has 3 EditTexts. Now when I close the app and return it, the value in EditText are gone and I have to return the values last entered.

How can I return those values entered by the user in EditText (On the press of a button) so that the user don't have to enter the whole text again and again on closing and responding of the app?

Providing the answer with the code will help a lot! Thanks.

Upvotes: 0

Views: 1259

Answers (2)

raktale
raktale

Reputation: 465

Put this class in your project

public class SaveData {

private static final String EDIT1 = "edit1";
private static final String EDIT2 = "edit2";
private static final String EDIT3 = "edit3";
SharedPreferences pref;
Editor editor;
Context mContext;
// Shared pref mode
int PRIVATE_MODE = 0;

public SaveData(Context context) {
    this.mContext = context;

    // Sharedpref file name
    final String PREF_NAME = mContext.getString(R.string.app_name) + "_pref";
    pref = mContext.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    editor = pref.edit();
}

public String getEditText1() {
    return pref.getString(EDIT1, "");
}

public void setEditText1(String text)  {
    editor.putString(EDIT1, text);
    editor.commit();
}

public String getEditText2() {
    return pref.getString(EDIT2, "");
}

public void setEditText2(String text){
    editor.putString(EDIT2, text);
    editor.commit();
}

public String getEditText3() {
    return pref.getString(EDIT3, "");
}

public void setEditText3(String text) {
    editor.putString(EDIT3, text);
    editor.commit();
}
}

Now in onCreate put this

SaveData saveData = new SaveData(this);
mEditText1.setText(saveData.getEditText1());
mEditText2.setText(saveData.getEditText2());
mEditText3.setText(saveData.getEditText3());

And in onPause

SaveData saveData = new SaveData(this);
saveData.setEditText1(mEditText1.getText()+"");
saveData.setEditText2(mEditText2.getText()+"");
saveData.setEditText3(mEditText3.getText()+"");

Upvotes: 1

Mark Gilchrist
Mark Gilchrist

Reputation: 2032

There are several ways to store data of this type, I would personally use a database table, especially if you are ready have a database as part of your application. If not consider writing and array of strings to file and reading it.

If you are looking at the Database option the do something like

// on create
String createTable = "CREATE TABLE texts (id INT NOT NULL,words TEXT)";
SQLiteDatabase db =  MyDatabaseHelper.getDB(); // <- use the method you use to get a db.
db.exec(createTable);
db.close();


 // on start up of your edit text activity

 // create array of edit texts which you have initialized via their ids, it must be a member variable
 EditText[] editTexts = new EditText[]{editText1,editText2,editText3};

 SQLiteDatabase db;
 String sql= "SELECT * from texts";
 Cursor c = db.rawQuery(sql,null);
 if(null !=c && c.moveToFirst())
 {
      for(int i = 0;  i<c.getCount();  i++)
      {
          // get data from db
          String text = c.getString(1);
          int id = c.getInt(0);
          editTexts[id].setText(text);
      }
 }



// to be called at the end of the activity or when then edit texts change
private void saveToDb(){
    SQLiteDataBase db; // got from your sqlite helper method

    for(int i =0; i<editTexts.length; i++)
    {
        // check for insert
        String check = "SELECT *  FROM texts WHERE id ="+i;
        String ins;
        Cursor c = db.rawQuery(check,null);
        if(null != c && c.moveToFirst()){
        // update
           ins= "UPDATE texts SET words = '"+editTexts[i].getText().toString+"' WHERE id = "+i+";";
        }else{
        // insert
           ins = "INSERT into texts (id,words) VALUES("+i+",'"+editTexts[i].getText().toString+"');";
        }
        db.exec(ins);
        db.close();
    }
}

I have written this without testing it as a guide line, I have used this pattern many times before and it works well

Upvotes: 0

Related Questions