Going back to activity with EditTexts populated. - Android

417 Views Asked by At

I have Activity A which has editTexts and from activity A, a user can go to activity B and fill out more information. Once the user is done on activity B, they press a button and come back to A. I would like to come back to A with the information that the user had filled in to be already there and not have to fill in the information again.

Currently, this is my code for coming back from B to A.

                Intent intent = new Intent(NewAddressActivity.this, CreditCardActivity.class);
                intent.putExtra("newAddressEntered", true);
                intent.putExtra("newAddress", (Serializable) newAddress);
                startActivity(intent);

With this code, when the user comes back, the fields on activity A are empty.

*Note - I need to pass data from B to A when going back.

Any tips on how to do this properly would be greatly appreciated.

Thanks

3

There are 3 best solutions below

2
On

You're actually not "coming back", but starting a new empty intent, thats why the fields are blank again. Instead, you should just call:

finish()

Regarding filling related fields from different Activities, I wouldn't recommend that, but if you need to keep it that way, you may have a singleton class to store all field values for instance.

Also, you could send B fields to A as activity results.-

http://developer.android.com/training/basics/intents/result.html

But still, I would recommend keeping all related fields in one Activity.

0
On

You are on the right track. You can do that using .putExtra

You can pass extra data via the intent using intent.putExtra("key", text_field.getText().toString()) on the intent before you send it (in the first activity) and getIntent().getExtras().getString("key") in the second activity.

This is assuming text_field is your EditText you want to pass the value from. You can change "key" to whatever you want, too.

0
On

Change the launchmode of your Activity A (CreditCardActivity) to singleTop in manifest file and try. The documentation says

If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity.

So your activity should retain old values.