0

If Activity A calls Activity B with .setExtra(someKey, someValue), and Activity B calls Activity C, from which the user returns to Activity B with the back button, can Activity B still access the Extras from Activity A?

  1. A
  2. A -> B (with Extra)
  3. A -> B -> C back button pressed
  4. A -> B (with Extra?)

Is the Bundle persisted or discarded in this scenario?

I could not find this in When do intent extras become null between activities? and Android Intent Extras Sticking Around

serv-inc
  • 35,772
  • 9
  • 166
  • 188

2 Answers2

1

In ActivityB when calling getIntent() you will always have access to the "extras" that were sent in the Intent that started ActivityB. Android persists the Intent (including "extras") so that even if Android kills the process (because the user put it in the background), when the user returns to the app, Android creates a new process and recreates the Activity with the original Intent.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 1
    You need to read the Android source code. Here are some references in the official documentation: In https://developer.android.com/topic/libraries/architecture/saving-states in the section on "system-initiated process death" it says "Additionally, when you open an activity from an intent, the bundle of extras is delivered to the activity both when the configuration changes and when the system restores the activity. " – David Wasser Jun 10 '18 at 17:52
-2

No, they do not always persist. Most of the time, it works, but sometimes, the extra was just null, which created a NullPointerException upon accessing it. So there is no guarantee that the system keeps the Extra. Better save it somewhere.

serv-inc
  • 35,772
  • 9
  • 166
  • 188
  • Of course they are persisted. If you are getting `null` then there is some other problem. – David Wasser Jun 08 '18 at 16:45
  • @DavidWasser: it always worked when entering B for the first time, some times, the extra was gone when returning from `C`: the code worked most of the time, though. Do you want to look at the code? – serv-inc Jun 08 '18 at 18:32
  • 1
    Sure. There should be no reason that it doesn't work when returning from C. – David Wasser Jun 10 '18 at 17:47