Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How long does a Bundle last?

I know that a Bundle can be used to pass data between activities, as well as for saving and restoring data in cases of configuration changes like a screen rotation.

I also read that SharedPreferences is the preferred way to store persistent data, rather than a Bundle.

Why exactly is this the case? In order for a Bundle to handle a configuration change, it would have to persist even after an activity is destroyed. So what exactly is the lifespan of a Bundle? Is there a certain point where it arbitrarily gets eliminated by garbage collection, or does a Bundle's data persist for as long as you don't manually choose to clear it?

like image 492
ryye Avatar asked Sep 08 '15 09:09

ryye


1 Answers

Bundle lasts while the Activity exists, aka it has not been finished.

An interesting fact to note is that when process death occurs and Android massacres your application and the Application class is recreated (onCreate() is called), then the Activity stack is reconstructed, and the activity is reinitialized from the saveInstanceState bundle.

The SharedPreferences literally stores the data you give it in an XML file in the data/<applicationname>/preferences folder, if I remember the location right.

like image 79
EpicPandaForce Avatar answered Oct 22 '22 08:10

EpicPandaForce