variables need to be saved

34 views Asked by At

I am making an application that contains a form and whenever a data clicks a button loads from a BD in a EditText, but every time I press a different button, the other EditText are cleared, I tried with:

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString("data", myVariable);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
   super.onRestoreInstanceState(savedInstanceState);
   other = savedInstanceState.getString("data");
   name.setText(other);

}

Sorry if I have not explained well, I need that every time they change Activity variables and I have not deleted. Any suggestions? Thank you!

1

There are 1 answers

0
Hadi Satrio On BEST ANSWER

Try using Android SharedPreferences instead. This is a persistent key-value storage that Android offers for its apps and was designed to covers issues like this. Here's the easy way to use it:

SharedPreference prefs = PreferenceManager.getDefaultSharedPreferences(this);

// To put a data (in this case a String from your EditText)..
Editor editor = prefs.edit();
editor.putString("data", yourStringHere);
editor.commit();

...

// ..and to retrieve it..
prefs.getString("data", null);

// NOTE: The 'null' in above method call could be replaced by any String you want;
// it basically specifies a default value to get when "data" is empty or doesn't 
// yet exist.

Hope this helps.