Title: How do I retain the contents of a EditText in MainActivity when I proceed to a child activity and return back to MainActivity?
Tags: android;android-intent
Question: I'm trying to retain the contents of EditText in MainActivity when I return back to MainActivity after proceeding to its child activity.
In MainActivity i have two EditText fields, one stores first name
other stores last name and a Button which when clicked invokes the newActivity() method. Then in the SecondActivity which is a Child of MainActivity the first as well as the last name is displayed but when I go bact to MainActivity the contents of EditText fields are not retained.

Below is the code I have written in MainActivity.java and SecondActivity.java

```    /************ MainActivity.java *************/

        public class MainActivity extends ActionBarActivity {

        private EditText firstNameField ;
        private EditText lastNameField ;
        private String firstName, lastName;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            firstNameField = (EditText) findViewById(R.id.firstNameField);
            lastNameField = (EditText) findViewById(R.id.lastNameField);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }


        public void newActivity(View view) {

            firstName = String.valueOf(firstNameField.getText());
            lastName = String.valueOf(lastNameField.getText());

            Intent secondActivity = new Intent(this, SecondActivity.class);
            secondActivity.putExtra("First name", firstName);
            secondActivity.putExtra("Last Name", lastName);
            secondActivity.putExtra("Last name", lastName);

            startActivity(secondActivity);


        }

        @Override
        public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
            outState.putString(FIRST_NAME_KEY, firstName);
            outState.putString(LAST_NAME_KEY, lastName);
            super.onSaveInstanceState(outState, outPersistentState);
        }

        public void onRestoreInstanceState(Bundle savedInstanceState) {
            // Always call the superclass so it can restore the view hierarchy
            super.onRestoreInstanceState(savedInstanceState);

            // Restore state members from saved instance
            firstName = String.valueOf(savedInstanceState.getString(FIRST_NAME_KEY));
            lastName = String.valueOf(savedInstanceState.getString(LAST_NAME_KEY));
            firstNameField.setText(""+firstName);
            lastNameField.setText(""+lastName);
        }
    }

/************** SecondActivity.java ***********/

public class SecondActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);

        String fname = String.valueOf(getIntent().getStringExtra("First name"));
        String lname = String.valueOf(getIntent().getStringExtra("Last name"));

        EditText firstNameEdt = (EditText) findViewById(R.id.firstNameEdt);
        EditText lastNameEdt = (EditText) findViewById(R.id.lastNameEdt);

        firstNameEdt.setText(""+ fname);
        lastNameEdt.setText("" + lname);
    }
}
```

Comment: I just posted u an answer please try it.


Here is another answer: You can save the values of your edittexts in ```SharedPreferences```. There you can save values persistent with a defined key. 

You would want to save the values in ```onDestroy``` of your MainActivity retrieve your values in ```onCreate```.

Here you can read how to use ```SharedPreferences```: Saving Key-Value Sets

Comment for this answer: Thanks a lot for your response..
Its late in my country right now so i'll try it tomorrow and let you know if it worked out
Comment for this answer: okay I went as per the Recreating Activity documentation on Android developer site.
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
It says by using onSaveInstanceState() method and onRecreateActivityMethod() i should be able to achieve the functionality I want.
But why it isn't working in my case ?

Here is another answer: For a simple method, if you can bring the contents to the second activity, why don't you just do the same as you go back to the 1st activity. Use bundle going back to the 1st activity then set it as a Text


Here is another answer: Most UI widgets take care of propagating their own state between activity instances.  If you comment out your ```onSaveInstanceState``` and ```onRestoreInstanceState``` methods, it'll probably just work.  You only need to implement those methods if your activity has its own state separate from the UI.

Comment for this answer: Can you give an example ?
I am a beginner so its difficult for me to understand what case you're talking about.
Comment for this answer: I went as per the Recreating Activity documentation on Android developer site.
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
It says by using onSaveInstanceState() method and onRecreateActivityMethod() i should be able to achieve the functionality I want.
But why it isn't working in my case ?
Comment for this answer: your method didn't work as well.
Commenting out both methods didn't make any difference

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:06/07/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<android><android-intent>', 'Title': 'How do I retain the contents of a EditText in MainActivity when I proceed to a child activity and return back to MainActivity?', 'ViewCount': 109, 'id': 30697040, 'user_AboutMe': "<p>I'm a novice coder looking forward to move fast and break things</p>\n", 'user_AccountId': 5917151.0, 'user_DisplayName': 'Abhishek Raina', 'user_Reputation': 13.0, 'user_Views': 19.0, 'user_id': 4658824.0}}