Search code examples
androidsharedpreferencesandroid-sharedpreferences

Save user input to SharedPrefernces and set Value to EdiText


I want to save user input to SharedPreferences when i click a button so that when a user launches the activity again the user input is set to the EditText and TextView. I already have a sharedPreferences File created that i save some text to in a previous activity. I want update the SharedPreferences File with another details from this new activity that i created.

This is code for creating and saving to SharedPreferences

private SharedPreferences prefs;
public static final String PREF_FILE_NAME = "AuthUser";

prefs = this.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
txtDate = (TextView) findViewById(R.id.txtDate);

    addIdButton = (Button) findViewById(R.id.btnAddId);
    addIdButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            expiryDate = txtDate.getText().toString();
            edtIdNumber = (EditText) findViewById(R.id.edtIdNumber);
            idNumber = edtIdNumber.getText().toString();
            SharedPreferences.Editor editor = prefs.edit();
            editor.putInt("idType", idType);
            editor.putString("idNumber", idNumber);
            editor.putString("expiryDate", expiryDate);
            editor.apply();
        }
    });

This is my code to set the value from shared preferences to EditText and TextView

@Override
public void onStart(){
    super.onStart();
    File f = new File(PREF_FILE_NAME);
    if (f.exists()){
        idType = prefs.getInt("idType", 0);
        idNumber = prefs.getString("idNumber", "");
        expiryDate = prefs.getString("expiryDate", "");

        edtIdNumber = (EditText) findViewById(R.id.edtIdNumber);
        txtDate = (TextView) findViewById(R.id.txtDate);
        txtDate.setText(expiryDate);
        edtIdNumber.setText(idNumber);
        edtIdNumber.setText(idNumber);
        txtDate.setText(expiryDate);

    }
}

Solution

  • File f = new File(PREF_FILE_NAME); just has the name of the preference so f.exists() will always returns false

    Solution : Remove

    File f = new File(PREF_FILE_NAME);
    if (f.exists()){
    

    because if you don't have any value then simply use the default value

    @Override
    public void onStart(){
        super.onStart();
        //File f = new File(PREF_FILE_NAME); // not a right path
        //if (f.exists()){ // false
            idType = prefs.getInt("idType", 0);
            idNumber = prefs.getString("idNumber", "");
            expiryDate = prefs.getString("expiryDate", "");
    
            edtIdNumber = (EditText) findViewById(R.id.edtIdNumber);
            txtDate = (TextView) findViewById(R.id.txtDate);
            txtDate.setText(expiryDate);
            edtIdNumber.setText(idNumber);
            edtIdNumber.setText(idNumber);
            txtDate.setText(expiryDate);
    
        //}
    }
    

    Alternatively you can use contains