So i have been bundling my data between my activities just fine and recently started to save my data using shared preferences but now i'm wondering if it would be easier on the phone if i saved and loaded shared preferences between the activities or if bundling my data between them and using shared preferences to save it when they close the app is a better choice? My data i'm using is just simple data like a few ints strings and Boolean data.
Let us now see the various methods in shared preferences in android: contains ( String key): It checks if the preference contains a preference or not. edit (): It creates a new Editor for preferences, to enable modifications to the data and commit changes back to the SharedPreferences atomically.
getDefaultSharedPreferences () : used on the PreferenceManager, to get the shared preferences that work in concert with Android’s overall preference framework In this tutorial we’ll go with getSharedPreferences ().
@Sultan Shared Preferences are shared between all activities in the same Application (package). If you get a SharedPreferences.Editor and commit some value then launch a new Activity, that Activity can get a SharedPreferences object and retrieve that value using the same given key (a string identifying it).
This is done as follows: SharedPreferences sharedpreference_name = getSharedPreferences (String name, MODE_PRIVATE); In this method, we’ve passed two parameters: the first parameter is the key name of the SharedPreference and the second parameter is the MODE.
If you're using SharedPreferences anyways because you have to keep the values when the app is closed, then just use SharedPreferences all of the time.
However, to save storage space on the user's phone, use a bundle if you only need to share the data between activities and it can be scrapped after the app is closed.
Bundles are for used mainly for passing data between activities. If the data that you're storing is in the form of settings for example, then you're better off using sharedpreferences. If you are storing more data than just a couple of ints and what have you, you should use a SQLite database.
To communicate between activities you can use an Bundle, but if you have a large amount of data i suggest the use of preferences. To preserve your data when the app is closed use SharedPreferences or a DataBase implementation.
Android Saving Data
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