Making Data Persistent in Android

Making data persistent in android

Many applications may provide a way to capture user preferences on the settings of a specific application or an activity. For supporting this, Android provides a simple set of APIs.

Preferences are typically name value pairs. They can be stored as “Shared Preferences” across various activities in an application (note currently it cannot be shared across processes). Or it can be something that needs to be stored specific to an activity.

  1. Shared Preferences: The shared preferences can be used by all the components (activities, services etc) off the applications.

  2. Activity handled preferences: These preferences can only be used with in the activity and can not be used by other components of the application.

Shared Preferences:

The shared preferences are managed with the help of the getSharedPreferences method of the Context class. The preferences are stored in a file, that can be either a custom one (1) or the default file (2).

(1) Here is how you get the instance when you want to specify the file name

public static final String PREF_FILE_NAME = "PrefFile";
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);

MODE_PRIVATE is the operating mode for the preferences. It is the default mode and means the created file will be accessed by only the calling application. Other two mode supported are MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE. In MODE_WORLD_READABLE other application can read the created file but can not modify it. In case of MODE_WORLD_WRITEABLE other applications also have write permissions for the created file.

(2) The recommended way is to use by the default mode, without specifying the file name:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

Finally, once you have the preferences instance, here is how you can retrieve the stored values from the preferences:

 int storedPreference = preferences.getInt("storedInt", 0);

To store values in the preference file SharedPreference.Editor object has to be used. Editor is the nested interface of the SharedPreference class.

SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();

Editor also support methods like remove() and clear() to delete the preference value from the file.

Activity Preferences:

The shared preferences can be used by other application components. But if you do not need to share the preferences with other components and want to have activities private preferences. You can do that with the help of getPreferences() method of the activity. The getPreference method uses the getSharedPreferences() method with the name of the activity class for the preference file name.

Following is the code to get preferences:

SharedPreferences preferences = getPreferences(MODE_PRIVATE);
int storedPreference = preferences.getInt("storedInt", 0);

The code to store values is also same as in case of shared preferences.

SharedPreferences preferences = getPreference(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();

You can also use other methods like storing the activity state in database. Note Android also contains a package called android.preference. The package defines classes to implement application preferences UI.

To see some more examples check Android's Data Storage post on developers site.

How to keep data persistent throughout the application

I think the way I am going to keep the data accessible throughout the application is to use an SQLite database which will store the data.

Using the XML file, I will parse the data and put it in the database on first launch using a created subclass of SQLiteOpenHelper. When the data is needed I will make queries to the database using the subclass using read access. Each component (Activity/Service/etc.) would have its own instance of the SQLiteOpenHelper to make queries to the database.

How to store persistent data easily without using databases?

Depending of your context (especially but not only related to the size of your data), you may want to use the Shared Preferences, the internal storage or the external storage (the SD card).

An official guide about that is available here.

How can I make Android application data persistent?

The table definitely stays if the user simply reboots the phone. Off the top of my head, I think the only way to get rid of the table completely is to either:

  • Delete it (programmatically or via the sql command line)
  • Uninstall the application

Simply upgrading your application to a new version will not delete your existing tables (so long as you use the same keystore when making the APK.)

-- Dan

What's the method for storing arrays or custom objects (persistent data)?

You could serialize the array and store it to the shared preferences, similar to the answer to this question:

Android Sharepreferences and array

To store multiple profiles you could possibly store each in the shared preferences with a key like "Profile-1", "Profile-2", etc. You could then have another shared preference that has the number of Profiles you have.

Persistent Data Storage in Android Development

As you said in your question itself you can use file or database.If more data is there then you can go for file and database(SQLITE) or else if small data then you can also go for SharedPreferences.Below is one link where its all about datastorage in android all from documentaion.Hope it will help you. :)

Here is link

Best way to persist data between orientation changes in Android

I will suggest you to fix the orientation from manifest and through program if orientation configuration changes(you can add listener) you can adjust your views as it looks like orientation has been changed. In this way you can save your data as well unnecessary memory storage. Aligning and repositioning views dynamically won't be vague don't worry.

  // Add inside manifest.
android:configChanges="orientation|keyboardHidden"

// Reposition your views
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
align_landscape();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
align_portrait();
}
}


Related Topics



Leave a reply



Submit