0

I have two EditText fields (suppose they are empty) and I will write the text there and it will be remembered there, in the sense that when I go out of the window and go back there will be text entered there. Thank you for your help :)

Button button1;
Button button2;
TextView devices;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.change_url);
    button1 = (Button) findViewById(R.id.button9);
    button2 = (Button) findViewById(R.id.button10);


    button2.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            TextView buildings = (TextView) findViewById(R.id.buildings);
            String a = buildings.getText().toString();
            buildings.setText(a);
        }
    });

}
ollaw
  • 2,086
  • 1
  • 20
  • 33
Torwopik
  • 19
  • 5
  • 4
    Possible duplicate of [Saving Android Activity state using Save Instance State](http://stackoverflow.com/questions/151777/saving-android-activity-state-using-save-instance-state) – ollaw May 14 '17 at 22:25

2 Answers2

0

As my point of view, Store your value in shared preference and whenever you want this values get from shared preference and put in edit text.

nivesh shastri
  • 430
  • 2
  • 13
0

When you go out of a activity and you want to recover data when you come back, you should override onSaveInstanceState(Bundle savedInstanceState). Inside this method you will need to save all the data you want to recover when you come back again in the Bundle.

As example for your case:

First:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is killed and restarted.
  savedInstanceState.putString("EditText1", mEditText1.getText().toString());
  savedInstanceState.putString("EditText2", mEditText2.getText().toString());
}

Then, to recover it you need to override onRestoreInstanceState(Bundle savedInstanceState) :

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  String text1 = savedInstanceState.getString("EditText1");
  String text2 = savedInstanceState.getString("EditText2");
  //At this point you recovered your data, so you can put in the EditText
  mEditText1.setText(text1);
  mEditText2.setText(text2);
}

More references at : https://developer.android.com/guide/components/activities/activity-lifecycle.html

Shudy
  • 7,806
  • 19
  • 63
  • 98