• Save
Android SharedPreferences
Upcoming SlideShare
Loading in...5
×
 

Android SharedPreferences

on

  • 1,287 views

 

Statistics

Views

Total Views
1,287
Views on SlideShare
946
Embed Views
341

Actions

Likes
0
Downloads
0
Comments
0

26 Embeds 341

http://vkedco.blogspot.com 162
http://www.vkedco.blogspot.com 108
http://vkedco.blogspot.in 21
http://vkedco.blogspot.co.uk 7
http://vkedco.blogspot.ca 5
http://vkedco.blogspot.fr 4
http://vkedco.blogspot.it 4
http://www.vkedco.blogspot.com.br 3
http://vkedco.blogspot.ru 3
http://vkedco.blogspot.de 3
http://vkedco.blogspot.tw 2
http://vkedco.blogspot.mx 2
http://www.vkedco.blogspot.ro 2
http://www.vkedco.blogspot.gr 2
http://vkedco.blogspot.pt 2
http://vkedco.blogspot.com.es 1
http://vkedco.blogspot.com.br 1
http://www.vkedco.blogspot.jp 1
http://www.blogger.com 1
http://vkedco.blogspot.ro 1
http://vkedco.blogspot.gr 1
http://vkedco.blogspot.com.au 1
http://www.vkedco.blogspot.no 1
http://www.vkedco.blogspot.it 1
http://www.vkedco.blogspot.de 1
http://vkedco.blogspot.hu 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Android SharedPreferences Android SharedPreferences Presentation Transcript

  • Android SharedPreferences Vladimir Kulyukinhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  • Outline ● Shared Preferences ● Persisting States with Shared Preferences ● Inter-Activity Communication with Shared Preferenceshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • Shared Preferenceshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • Data Persistence Techniques ● Local Files ● Shared Preferences ● Content Providers ● Cloud servershttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • Shared Preferences ● Lightweight key/value pair mechanism for saving primitive application data (Boolean, string, float, long, and integer) ● Commonly used for saving users application preferences, UI states, application settings ● Instances of SharedPreferences class can contain key/value pairs that can be shared among all activities running within the same application contexthttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • Accessing Shared Preferences ● Shared preferences are key/value pairs are that are persisted against the applications context ● Any application component such as activities, services, and broadcast receivers can access those values by getting the applications contexthttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • Getting SharedPreferences Object // Use PreferenceManager and the applications context to initialize // a SharedPreferences object inside a component SharedPreferences mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • Example 01 Implement an application, SharedPrefsApp01, that allows the user the enter strings for two shared preferences via EditTexts and saves those shared preferences. The next time the application is started, the previously entered values are loaded into the EditTexts from the saved shared preferences. The main activity has three buttons: Save, Clear, and Finish. Click on Save reads the two strings entered by the user and saves them as shared preferences under appropriate keys. Clear clears the EditTexts, Finish finishes the activity by calling Activity.finish().http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • Example 01: Screen Snapshots source is herehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • Activity Reference Variables // SharedPreferences, Editor to store and save // shared preferences as key/value pairs SharedPreferences mSharedPrefs = null; Editor mSharedPrefsEditor = null; Resources mRes = null;http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • Example 01 Code Snippetshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • Persisting Shared Preferences // grab the strings entered by the user, put them into the SharedPreferences // Editor under appropirate keys and persist them via commit(). private void saveSharedPrefs() { mSharedPrefsEditor.putString(mRes.getString(R.string.pref_01_key), mEdTxtPrefVal01.getText().toString()); mSharedPrefsEditor.putString(mRes.getString(R.string.pref_02_key), mEdTxtPrefVal02.getText().toString()); mSharedPrefsEditor.commit(); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • Loading Shared Preferences // Load up the values of the appropriate keys from SharedPreferences and // default to “” if they are not defined.. private void loadSharedPrefs() { this.mEdTxtPrefVal01 .setText(this.mSharedPrefs .getString(this.mRes.getString(R.string.pref_01_key), "")); this.mEdTxtPrefVal02 .setText(this.mSharedPrefs .getString(this.mRes.getString(R.string.pref_02_key), "")); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • Inter-Activity Communication ● Components within the same application can use shared preferences to pass information to each other ● A typical use case is for component A to persist several key/value pairs in as shared preferences ● Component B then uses shared preferences to read key/value pairs and do something depending of specific values.http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • Example 02 Implement an application, SharedPrefsApp02, that allows the user the enter strings for two shared preferences via EditTexts and saves those shared preferences in the activity SharedPreferencesSaverAct. This activity has four buttons: Save, Pass, Clear, and Finish. The logic of Save, Clear, and Finish is the same as in Example 01. Pass button launches another activity, SharedPreferencesReceiverAct. This activity loads the values persisted by the user with shared preferences in SharedPreferencesSaverAct.http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • Example 02: Screen Snapshots source is herehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • Example 02 Code Snippetshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • Persisting Shared Preferences in SharedPreferencesSaverAct // grab the strings entered by the user, put them into the SharedPreferences // Editor under appropirate keys and persist them via commit(). private void saveSharedPrefs() { mSharedPrefsEditor.putString(mRes.getString(R.string.pref_01_key), mEdTxtPrefVal01.getText().toString()); mSharedPrefsEditor.putString(mRes.getString(R.string.pref_02_key), mEdTxtPrefVal02.getText().toString()); mSharedPrefsEditor.commit(); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • Launching SharedPreferncesReceiverAct in SharedPreferencesSaverAct // SharedPreferencesReceiverAct is launched on a click on the Pass button mBtnPass.setOnClickListener( new OnClickListener() { public void onClick(View v) { Intent i = new Intent(getApplicationContext(), SharedPrefsReceiverAct.class); startActivity(i); } });http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • Loading Shared Preferences in SharedPreferncesReceiverAct // Look up appropriate values and default to empty strings if they are not // defined. private void loadSharedPrefs() { mEdTxtPrefVal01.setText(this.mSharedPrefs.getString(mRes.getString(R.string.pref_01_key), "")); mEdTxtPrefVal02.setText(this.mSharedPrefs.getString(mRes.getString(R.string.pref_02_key), "")); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • References ● http://developer.android.com/reference/android/content/SharedPreferences.htmlhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com