Search code examples
javaandroidandroid-sharedpreferences

How to save textfield response without clearing


I am trying to create a simple to do list style app. I have successfully saved the string using SharedPreferences, as I am able to refer to it in the Logs, but I would like to make it so that the string stays attached to the textfield instead of being cleared every time app is reopened (until changed or deleted by the user). Any other suggestions would be appreciated.

(MainActivity)

public void go(View view) {

    Intent intent1 = new Intent(SecondActivity.this, myPreferences.class);

    String passGoal1 = goal1.getText().toString();

    intent1.putExtra("goal1", passGoal1);

    startActivity(intent1);
}

Also implemented myPreferences class as referenced in the first answer.

public class myPreferences {

    private SharedPreferences pref;

    private SharedPreferences.Editor editor;

    private Context context;

    int PRIVATE_MODE = 0;

    private static final String PREF_NAME = "MyPreferencesName";

    public final static String KEY_NAME = "key_value";

    public myPreferences(Context context) {

        this.context = context;

        pref = context.getSharedPreferences(PREF_NAME,PRIVATE_MODE);

        editor = pref.edit();
    }

    public void setFunction(String data) {

        editor.putString(KEY_NAME, data);
    }
}

Solution

  • In your shared preferences make a get function that fetch the stored data and view it every time in textfield

    public class MyPreferences {
    
    private SharedPreferences pref;
    
    // Editor for Shared preferences
    private SharedPreferences.Editor editor;
    
    // Context
    private Context context;
    
    // Shared pref mode
    int PRIVATE_MODE = 0;
    
    // Sharedpref file name
    private static final String PREF_NAME = "MyPreferencesName";
    
    public final static String KEY_NAME = "key_value";
    
    public MyPreferences(Context context) {
        this.context = context;
        pref = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }
    
    public void setFunction(String data) {
        editor.putString(KEY_NAME, data);
        editor.commit();
    }
    
    public String getFunction() {
        return pref.getString(KEY_NAME, "");
    }
    }
    

    And in your activity initialize SharedPreferences and do the following:

    private MyPreferences myPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        myPreferences = new MyPreferences(this);
    
        //To set EditText data from SavedPreferences
        textField.setText(myPreferences.getFunction());
    }
    
    //To save the latest data from EditText.
    @Override
    protected void onStop() {
        Log.d("LifeCycle", "onStop: ");
        super.onStop();
        myPreferences.setFunction(editText.getText().toString());
    }