1

How can I preserve data between activities? For instance, I have an edit text and a button in Activity A. I fill in that editText and then click on the button. That button starts a new intent, Activity B. In B there are several buttons, but all of them if clicked on, start activity A again. I want that editText to be still filled in as well as pass specific data to Activity A depending on the button pressed in Activity B. I know this has to do with onStop() -> onStart(), but couldn't get it to work.

Pseduosance
  • 305
  • 1
  • 5
  • 16

2 Answers2

0

You should start Activity B with startActivityForResult. Then, when u set the result, you should "catch" it in Activity A by overriding onActivityResult().

As for EditText, if you don't call finish() on Activity A - you don't need to override onSaveInstanceState or anything else - system does it for you.

localhost
  • 5,568
  • 1
  • 33
  • 53
  • It doesn't save the EditText for me, I never called finish, but when I go to Activity A from B, the text is gone. I'm not simply going back when I'm in Activity B, I end up making a new intent to go to Activity A and starting that intent. Is there some way to just start the previous intent? Is that what startActivityForResult does? – Pseduosance Jul 12 '15 at 23:59
  • You start new instance of Activity A, of course EditText will be clear. You should use startActivityForResult in Activity A, and setResult() and then finish() in Activity B. – localhost Jul 13 '15 at 00:05
0

Don't finish the activity in Activity A intent to Activity B. and use startActivityForResult(intent) rather than startActivity(intent) to be able to pass the data back to activity A again. check this answer to how How to manage `startActivityForResult` on Android?.

Community
  • 1
  • 1
Ahmed Hegazy
  • 12,395
  • 5
  • 41
  • 64