30

I am building an android application, and would like to ask if the intent that started an activity (which is reachable via the getIntent () method) is saved / preserved during activity recreation such as a device orientation, or simply the fact that android can erase the application state if it is in the background and rebuild it (if it is low on memory) ?

This is a quick example that illustrates the question :

The application launches activity A. Then the user clicks on a button that starts a new activity B. Activity A has sent to activity B a string X with the value Hello via the intent (using the putExtra method).

In activity B, I can retrieve the content of string X by retrieving the intent (via the getIntent () method) and then retrieving the string content (via the getStringExtra method).

Will I still be able to retrieve the extra string from intent, or even the intent itself if the activity is recreated due to device rotation, ... ?

Or should I save the extra string in the onSaveInstanceState method ?

I have tried the device rotation scenario, and the intent (along with the extra string) are always accessible.

Leeeeeeelo
  • 4,333
  • 3
  • 34
  • 44

1 Answers1

36

Will I still be able to retrieve the extra string from intent, or even the intent itself if the activity is recreated due to device rotation

Yes. You will have the same Intent (or, at least, a copy of the Intent) after the configuration change as you had before the configuration change.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 8
    Not just configuration changes, what about if the application goes to the background and then Android deletes the application instance to free some memory ? When it is restored, will the intent (or a copy like you pointed) and its extra data be available ? – Leeeeeeelo Mar 15 '13 at 08:34
  • 9
    @Leeeeeeelo: "When it is restored, will the intent (or a copy like you pointed) and its extra data be available ?" -- that depends on how it is "restored". If the user just taps on the launcher icon, then no. If the user uses the recent-tasks list, then yes. – CommonsWare Mar 15 '13 at 15:41
  • Thank you for your prompt answer ! – Leeeeeeelo Mar 18 '13 at 08:41
  • @CommonsWare "If the user just taps on the launcher icon, then no" - is there way to get the original intent data in such scenario ? Would it be available via 'onSaveInstance/onRestoreState' mechanism ? – kiruwka Feb 17 '15 at 14:34
  • @kiruwka: No. If the app is no longer in the recent-tasks list, all ephemeral data (`Intent`, saved instance state, etc.) is gone. If you need data to survive, persist it somewhere. – CommonsWare Feb 17 '15 at 14:39
  • @CommonsWare Thanks. As side effect of what you explain, I assume you have to always null check your getIntent() before accessing it in onCreate, (even my activities always start with intent), am I correct ? – kiruwka Feb 17 '15 at 14:50
  • @kiruwka: Well, the `Intent` will not be `null`. *Some* `Intent` was used to start up the activity. The original question was about extras. If the same activity can be started both with and without extras, whether there are any extras will vary, and you have to check for that. Really, that has nothing to do with process termination or recent-tasks lists or anything like that. – CommonsWare Feb 17 '15 at 14:53
  • @CommonsWare Thanks again. Ok, let me confirm one thing. When using `intent` extras to perform activity initialization, one `can not` rely on them being ALWAYS there and have to check it in onCreate() each time (even though they are there in 99% cases including configuration change as in your answer). – kiruwka Feb 17 '15 at 14:58
  • @kiruwka: *If* the app is being restarted at a point dictated by the recent-tasks list, *then* the `Intent` will be whatever was used to start up the activity earlier and should have whatever extras it had then. *If* the app is being started otherwise, *then* it will have whatever extras are on the `Intent` being used to start the app at that point in time. Apps do not start randomly; something starts them, and that "something" will either have extras on the `Intent`, or not. – CommonsWare Feb 17 '15 at 15:11
  • 1
    @CommonsWare I was referring to background process killed due to memory constraint scenario, for which you explained extras won't be preserved if user restarts the process (and top stack activity) by tapping the icon. What extras does activity under question then receives ? Am I even correct in my assumption that this mostrecent activity (along with all the preceding activities in the stack) would be started ? Do you have such scenario explained in your book ? Thanks again. – kiruwka Feb 17 '15 at 15:34
  • @kiruwka: "you explained extras won't be preserved if user restarts the process" -- again, that depends on whether the app is in the recent-tasks list or not. "What extras does activity under question then receives ?" -- if the app is not in the recent-tasks list, the launcher activity will start up, with an `Intent` that AFAIK has no extras (though technically that's really up to the home screen implementation). If the app is in the recent-tasks list, the top-most activity on the stack is started, with an `Intent` with whatever extras it may have had when the activity was originally created. – CommonsWare Feb 17 '15 at 15:36
  • @kiruwka: Also note that I have not tested the behavior on Android 5.0, where the recent-tasks list has changed (for example, it survives a reboot). I do not have a chapter on the book on task management, as it makes my head hurt, though I will hopefully address it this year. – CommonsWare Feb 17 '15 at 15:39
  • 1
    @CommonsWare Well, it is quite reasonable to me that by removing app from recent-task list user also "resets" its state, so it will only start launcher activity(and not my current activity). So, to conclude : when recent-tasks list is intact, it is safe to assume that my current activity will ALWAYS receive same `intent` and extras in `onCreate`, correct ? I did not dig the code to support this claim nor seen it in the docs, but I definitely trust you. – kiruwka Feb 17 '15 at 15:43
  • @kiruwka: "it is safe to assume that my current activity will ALWAYS receive same intent and extras in onCreate" -- I would phrase it as it *should* get the same `Intent` with the same extras. I would still practice a bit of defensive programming, to do something reasonable if the extras are not there (e.g., route the user back to some safer starting point). – CommonsWare Feb 17 '15 at 15:47
  • @CommonsWare "I would still practice a bit of defensive programming" - I agree, this is crucial in android development. Thanks for helping out and looking forward to your new book. – kiruwka Feb 17 '15 at 15:50
  • @CommonsWare, if it was really true that tapping on the launcher icon after the activity is destroyed in the background doesn't preserve the intent's extras, it doesn't seem to be true anymore. This is easily tested using the "Don't keep activities" switch in the Developer Options. They've been preserved in every test I've done. I've tested it on API levels 23 & 27. – Gumby The Green Aug 03 '19 at 07:28
  • @GumbyTheGreen: I agree. I have never seen a problem there. However, with something like 26,000 device models across 29 versions, defensive programming is a way of life in Android app development... :-) – CommonsWare Aug 03 '19 at 10:55