Many applications can provide a way to capture user settings in the settings of a specific application or activity. To support this, Android offers a simple set of APIs.
Preferences are usually pairs of name values. They can be stored as “General Preferences” in various actions in the application (note that at present they cannot be shared between processes). Or it may be something that needs to be preserved for a specific activity.
General preferences: General settings can be used by all components (actions, services, etc.) for applications.
Processed actions: these settings can only be used in action and cannot be used by other application components.
General preferences:
General settings are controlled using the getSharedPreferences method of the Context class. Settings are saved in the default file (1) or you can specify the file name (2), which will be used to refer to the settings.
(1) This is how you get an instance when specifying a 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 settings. This is the default mode and means that only the calling application will access the created file. Other two modes supported are: MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE. In MODE_WORLD_READABLE, another application can read the created file, but cannot modify it. In the case of MODE_WORLD_WRITEABLE, other applications also have write permissions for the created file.
(2) The recommended method is to use the default mode without specifying a file name:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
Finally, once you have an instance of preferences, here is how you can get the saved values from the settings:
int storedPreference = preferences.getInt("storedInt", 0);
To store values in a preference file, you must use the SharedPreference.Editor object. An editor is a nested interface of the SharedPreference class.
SharedPreferences.Editor editor = preferences.edit(); editor.putInt("storedInt", storedPreference);
The editor also supports methods such as remove () and clear () to remove the preference value from the file.
Activity Settings:
General settings can be used by other components of the application. But if you do not need to share preferences with other components and you want to have personal preferences. You can do this using the getPreferences () method of activity. The getPreference method uses the getSharedPreferences () method with the name of the activity class for the preference file name.
Below is the code to get the settings:
SharedPreferences preferences = getPreferences(MODE_PRIVATE); int storedPreference = preferences.getInt("storedInt", 0);
The code for storing values is also the same as in the case of general settings.
SharedPreferences preferences = getPreference(MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putInt("storedInt", storedPreference);
You can also use other methods, such as storing an activity state in a database. Note. Android also contains a package called android.preference. The package defines classes for implementing the application user interface.
To see a few more examples, check the Android Data Storage post on the developers website.
For more information, check out this link:
Permanent data storage in android