Saving State for EditText and Button Clicks

2019-08-02 05:24发布

问题:

I have 2 Activities. First Activity has EditText and Next Button. Second Activity has one Back Button. In First Activity I entered some texts in EditText and I hit Next Button. Now I'm in SecondActivity, If I press Back Button it will return to First Activity clearing the EditText fields. I want that EditText field to remain same. How to achieve these steps?

FirstActivity:

public class HomeActivity extends Activity {
EditText text;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    text = (EditText) findViewById(R.id.editText1);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(HomeActivity.this,Second.class);
            startActivity(i);
        }
    });
}

 @Override
 protected void onSaveInstanceState(Bundle outState) {
 // TODO Auto-generated method stub
 super.onSaveInstanceState(outState);

 CharSequence sequence = text.getText();
 outState.putCharSequence("savedText", sequence);
 }

 @Override
 protected void onRestoreInstanceState(Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 super.onRestoreInstanceState(savedInstanceState);
 CharSequence sequence = savedInstanceState.getCharSequence("savedText");
 text.setText(sequence);

 }
}

SecondActivity:

public class Second extends Activity {
Button b;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    b = (Button) findViewById(R.id.button_back);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(Second.this, HomeActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);
        }
    });

}

  }

回答1:

In this specific case, when you want to retain the values of your MainActivity, you just need to finish the SecondActivity instead of creating new intent for HomeActivity like

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    //Intent i = new Intent(Second.this, HomeActivity.class);
    //i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    //startActivity(i);
    //JUST FINISH THIS (SecondActivity) ACTIVITY
    finish();
}

This will close the second activity perminently, and Android Acticity Stack will show the previous activity with all its data.

In the more complex scenarios, when you have complex navigation between activities and still want data to retain in fields, you need to use SharedPreferances, save data when you want (idealy when navigating to other activity) and load again (in onResume()) when you get back to this activity.

Hope this helps..:)