I'm new to Android Studio and would like to save user input to an edittext And even if the user presses the back button or closes the application the typed text stays in edittext
Can anyone help me?
I'm new to Android Studio and would like to save user input to an edittext And even if the user presses the back button or closes the application the typed text stays in edittext
Can anyone help me?
Android, in turn, provides several forms of data persistence. These include:
External Rescue and Internal ;
In your case, we can use the simplest one, which is SharedPreferences . This method saves your data (values) in native system keys so that you can access it at any time you want. The way of using it is also very simple.
public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle state){
super.onCreate(state);
// Put code
// Regravar dados, se caso for necessário.
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String eValue = settings.getString("EditText_value", "xxxx");
}
@Override
protected void onStop(){
super.onStop();
// Este método faz com que, quando a aplicação for pausada, seja inserido um valor na Preference. Eu acredito que seja isto que você quer.
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("Edittext_value", myEditText.getText().toString()); // Isto é oque você quer.
// Salvar valor!
editor.commit();
}
Note : To write the value of your EditText, first get the value of it and pass it to SharedPreferences as a string.
For more information, see the Android documentation: Storage Options
You will need to control the information in this EditText using the lifecycle of your Activity on Android.
The ideal would be to persist the information of this EditText in the onPause () , onStop () .
Read a little and explore the life cycle of an activity can help you a lot.
have a very good pdf here