Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to save the Intent object in onSaveInstanceState() for later use?

The lifetime of an Intent object is unclear to me.

Can I always use the properties of the Intent object during the whole lifetime of an Activity? Or do I need to save the values passed to the Activity?

I do want to use the values (action and extra) of an Intent passed to the Activity during the complete lifetime of the Activity. I do not want to copy these values to variables of the Activity object. Why would I if I can get those values by calling 'getIntent()' from the Intent passed to the Activity.

I am wondering, when the Activity is destroyed by Android because to make room in memory, and later is restored again, is the Intent object still there by a call to 'getIntent()'? Or do I need to save the values of the Intent to the Bundle passed to the Activity in the 'onStateSaveInstanceState()'?

Thanks for your answer

like image 326
Peter de Leeuw van Weenen Avatar asked Oct 31 '22 11:10

Peter de Leeuw van Weenen


1 Answers

The lifetime of the intent is directly coupled to the lifetime of your Activity. If the Activity is destroyed the intent is not available anymore. Also the intent can be overwriten by another intent - depending on the flags that you set in your manifest for your Activity. If you really need the intent data after the Activity has been destroyed you need to save the data i.e. in your preferences.

like image 70
Carsten Avatar answered Nov 15 '22 03:11

Carsten