java - how to save & retrieve data in the same activity with SharedPreferences in Android -
i new android platform. please resolve query "how save & retrieve data in same activity sharedpreferences in android"
many applications may provide way capture user preferences on settings of specific application or activity. supporting this, android provides simple set of apis.
preferences typically name value pairs. can stored “shared preferences” across various activities in application (note cannot shared across processes). or can needs stored specific activity.
shared preferences: shared preferences can used components (activities, services etc) off applications.
activity handled preferences: these preferences can used in activity , can not used other components of application.
shared preferences:
the shared preferences managed of getsharedpreferences
method of context
class. preferences stored in default file(1) or can specify file name(2) used refer preferences.
(1) here how instance when specify file name
public static final string pref_file_name = "preffile"; sharedpreferences preferences = getsharedpreferences(pref_file_name, mode_private);
mode_private
operating mode preferences. default mode , means created file accessed calling application. other 2 mode supported mode_world_readable
, mode_world_writeable
. in mode_world_readable
other application can read created file can not modify it. in case of mode_world_writeable
other applications have write permissions created file.
(2) the recommended way use default mode, without specifying file name
sharedpreferences preferences = preferencesmanager.getdefaultsharedpreferences(context);
finally, once have preferences instance, here how can retrieve stored values preferences:
int storedpreference = preferences.getint("storedint", 0);
to store values in preference file sharedpreference.editor
object has used. editor
nested interface of sharedpreference
class.
sharedpreferences.editor editor = preferences.edit(); editor.putint("storedint", storedpreference); // value store editor.commit();
editor support methods remove()
, clear()
delete preference value file.
activity preferences:
the shared preferences can used other application components. if not need share preferences other components , want have activities private preferences. can of getpreferences()
method of activity. getpreference
method uses getsharedpreferences()
method name of activity class preference file name.
following code preferences
sharedpreferences preferences = getpreferences(mode_private); int storedpreference = preferences.getint("storedint", 0);
the code store values same in case of shared preferences.
sharedpreferences preferences = getpreference(mode_private); sharedpreferences.editor editor = preferences.edit(); editor.putint("storedint", storedpreference); // value store editor.commit();
you can use other methods storing activity state in database. note android contains package called android.preference
. package defines classes implement application preferences ui.
to see more examples check android's data storage post on developers site.
Comments
Post a Comment