Archive

Posts Tagged ‘Editor’

Android Basics – Managing Activity States – Part II

January 26th, 2009 2 comments

In the last post we saw how to use android provided onSaveInstanceState() method to manage the activity state. In this post we will see how we can use preferences to save the activity state.

Android provided two ways to use the preferences

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 getSharedPreferences method of the Context class. The preferences are stored in the file and file name is used to refer to the preferences.

Here is how you can retrieve the stored values from the preferences

...
public static final String PREF_FILE_NAME = "PrefFile";
...

SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);

 int storedPreference = preferences.getInt("storedInt", 0);
 // use the values

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.

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

...
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);

 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);
// use the values

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. We will see the preference package in some other post.