Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android shared preferences & bundle vs shared preferences [closed]

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.

like image 214
user2465356 Avatar asked Jan 23 '14 23:01

user2465356


People also ask

What are the methods in shared preferences in Android?

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.

What is getsharedpreferences() in Android?

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 ().

How to get shared preferences from one activity to another?

@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).

How do I get the shared preferences of a specific mode?

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.


3 Answers

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.

like image 138
Michael Yaworski Avatar answered Oct 18 '22 06:10

Michael Yaworski


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.

like image 22
Ogen Avatar answered Oct 18 '22 04:10

Ogen


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

like image 26
Jorgesys Avatar answered Oct 18 '22 06:10

Jorgesys