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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With