Top Banner
Cosc 5/4730 Android: “Dynamic” data and Preferences data.
37

Cosc 5/4730

Jan 19, 2016

Download

Documents

sadah

Cosc 5/4730. Android: “Dynamic ” data. Saving Dynamic data. While there are a lot of ways to save data Via the filesystem , database, etc. You can also save a “small” amount of dynamic data via a Bundle onCreate (Bundle savedInstanceState ) Temporary data sharedPreferences - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Cosc  5/4730

Cosc 5/4730

Android: “Dynamic” data andPreferences data.

Page 2: Cosc  5/4730

Saving Dynamic data.

• While there are a lot of ways to save data– Via the filesystem, database, etc.

• You can also save a “small” amount of dynamic data via a Bundle– onCreate(Bundle savedInstanceState)• Temporary data

– sharedPreferences• More permanent data.

Page 3: Cosc  5/4730

savedInstanceSate

• Override– public void onSaveInstanceState(Bundle

savedInstanceState) – void onRestoreInstanceState(Bundle

savedInstanceState)• But they are not in the Activity Lifecycle, so it

possible they will not get called!– Use Oncreate() as well.

Page 4: Cosc  5/4730

Another note.

• Configuration Changes– Unless you specify otherwise, a configuration

change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed and then restarted.

– See note at the end of this lecture about orientation.

• See Configuration Changes on– http://

developer.android.com/reference/android/app/Activity.html

Page 5: Cosc  5/4730

SavedInstance vs SharePreferences

• onSaveInstanceState(Bundle) activity event– This data will only be held in memory until the

application is closed, the data will be available any time that this activity starts within the current lifetime of the application.

• To store data between application instances (ie permanently) use SharedPreferences– This data is written to the database on the device

and is available all the time.

Page 6: Cosc  5/4730

Storing data

• Via OnsaveInstanceState and a bundle– A bundle has a set of get/put methods for strings, ints,

etc..– Using a key and data

public void onSaveInstanceState(Bundle savedInstanceState) { String d1 = t1.getText().toString(); savedInstanceState.putString("d1", d1); super.onSaveInstanceState(savedInstanceState);}

Page 7: Cosc  5/4730

Restoring data.

• Via OnCreate(Bundle) if (savedInstanceState != null) { //There is saved datad1 = savedInstanceState.getString("d1"); if (d1 != null) { //and d1 key exists t1.setText(d1); }}

Page 8: Cosc  5/4730

SharedPreferences

• Generally you want to store Preferences in OnPause()

• You restore preferences in OnCreate() and/or onPause()– Remember onPause() is called after OnCreate()– So it may not need to restore preferences in

OnCreate()• unless you use the data in OnCreate

Page 9: Cosc  5/4730

SharePreferences

• Also, you can set it up to share between activities or per activity.– The difference is the how you get the preferences– Activity preferences:

• SharedPreferences preferences = getPreferences(MODE_PRIVATE);

– Application preferences• SharedPreferences preferences =

getSharedPreferences(“example",MODE_PRIVATE);

– Both are private, but there is deprecated MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE, MODE_MULTI_PROCESS

Page 10: Cosc  5/4730

Storing

• In OnPause()Super.onPause(); //must be called• Get an spot to start the preferences and edit them.SharedPreferences preferences = getPreferences(MODE_PRIVATE);• ORSharedPreferences preferences = getSharedPreferences(“example",MODE_PRIVATE);• Now set it to editable.SharedPreferences.Editor editor = preferences.edit();

• Use the put methods with a key value and then commit them to memory

editor.putString("d3",d3);editor.commit();

Page 11: Cosc  5/4730

Restoring Preferences

• Since, I’m restoring in both onCreate() and OnResume()– I’ll use a separate method, called getprefs();– Note in OnResume(), you must call

super.OnResume()

Page 12: Cosc  5/4730

getPrefs()

void getprefs() {• Get the between instance stored valuesSharedPreferences preferences = getPreferences(MODE_PRIVATE);• ORSharedPreferences preferences = getSharedPreferences(“example",MODE_PRIVATE);

• Now read them back.d3 = preferences.getString("d3", "");

• Like preferenceActivity, we set a default value for the key– In this case “”, if the d3 key doesn’t exist.

t3.setText(d3);}

Page 14: Cosc  5/4730

Screen Orientation.

• As a note, remember you can set the screen orientation in via the manifest.xml file.

• In the <activity> tag• Put android:screenOrientation="portrait“– Or “landscape”

Page 15: Cosc  5/4730

PREFERENCES

Page 16: Cosc  5/4730

Preferences

• It’s normally a good idea to allow a user to setup some preferences for an application– Example: Username– It’s saved data between runs about how the

application should run.• Android provides a preferenceActivity and

PreferenceFragment to do this– Unlike everywhere else, preferences are not

supported (currently) in the supportv4 library.

Page 17: Cosc  5/4730

Preferences (2)

• The bulk of the work is in the xml document• The preferenceActivity (and fragment) only

provide a place to show the xml document.

Page 18: Cosc  5/4730

Preferences activity

• The activity itself is very easy.– Extend the prefrenceActivity and override the

onCreate – The preferences are kept in xml document.• Use the addPreferenceFromResourcesto use the xml file.

Page 19: Cosc  5/4730

Preferences activity (2)

• Code:public class preferences extends PreferenceActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences);

– This is depreciated in API 13+ in an activity, use a PreferenceFragment

}}

Page 20: Cosc  5/4730

PreferenceFragment• The code is basically the same, but in a fragment.public class PrefFrag extends PreferenceFragment {

@Overridepublic void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);addPreferencesFromResource(R.xml.preferences);

}}• A Note, there is no support.v4 version of this fragment

– At the end of the lecture, I’ll show how to combine them for backward compatibility.

Page 21: Cosc  5/4730

Preferences xml

• You need to create an xml for the preferences– Preferences are kept be application sessions by default.

• <PreferenceScreen …>– <PreferenceCategory …>– … in-line preference– </PreferenceCategory>– <PreferenceCategory …>– … </PreferenceCategory>

• </PreferenceScreen>

Page 22: Cosc  5/4730

CheckBoxPerference

• On (true) or off (false)<CheckBoxPreference android:key="checkbox_preference“

• key used by application to find out the value.

android:title="Check Box" android:summary="An example of a Check Box" />• Also, you can also set a default value as well.

– android:defaultValue=“false” Ie off by default

Page 23: Cosc  5/4730

EditTextPreference

<EditTextPreference android:key="edittext_preference" android:title="EditText Preference“ android:defaultValue="" android:summary="Example of EditText Dialog" android:dialogTitle="Enter your Name" />

Page 24: Cosc  5/4730

ListPreference

• <ListPreference– android:entries="@array/entries_list_preference"– android:entryValues="@array/

entryvalues_list_preference"– android:dialogTitle="list preference example“– android:summary="List preference demo" – android:key="list_preference"– android:title="list perference"

• />

Page 25: Cosc  5/4730

Arrays and xml

• In the values directory– created a file called

“arrays.xml”<resources><string-array name="entries_list_preference"> <item>Alpha Option 01</item> <item>Beta Option 02</item> item>Charlie Option 03</item> </string-array>

<string-array name="entryvalues_list_preference"> <item>alpha</item> <item>beta</item> <item>charlie</item> </string-array></resources>

Page 26: Cosc  5/4730

Access the preference

• In your java code, likely in onResume(…)– onResume is called when you app gets the screen

back from the preferences screen!– Also called when the application starts.– code look something like this:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences (getBaseContext());

– Now you get the preferences with • abstract Map<String, ?> getAll() OR• getBoolean(key, defaultvalue), getInt(key,defvalue),

getString(key, defaultvalue), …

Page 27: Cosc  5/4730

preference example

• prefs declared like on last slideboolean useSensor = prefs.getBoolean(“sensorPref” , false);– get the sensorPref key, if not found, default is false.

string text = prefs.getString(“textPref”, “”);string list = prefs.getString(“list_preference”,””);– Note, if you had set integer or floats as the

entryValues, then you want to get them as getInt or getFloat.

Page 28: Cosc  5/4730

Compatibility • Since there is no support.v4 fragment, you need to add a little extra code

in the fragmentActivitypublic void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {

addPreferencesFromResource(R.xml.preferences);}else{

//api 13+ and some “magic here”.getFragmentManager().beginTransaction().replace(android.R.id.content,

new PrefFrag()).commit();}• I don’t have any idea what android.R.id.content is, but there is no layout

for a fragmentActivity.

Page 29: Cosc  5/4730

Example code

• The PreferenceDemo has the code from all in.• The myPreferenceActivity and PrefFrag show

everything covered so far.– A note, the read of the preferences (in prefupdate

method in mainactivity), this perferences are set in PrefupdateActivity

• PrefupdateActivity and PrefupdateFrag show the next stuff.

Page 30: Cosc  5/4730

Preferences android:summary

• It maybe that instead of you want a the current value listed, instead of the descriptions as I have listed here.

• You will need to implement a listener in PreferenceActivity called OnSharedPreferenceChangeListener

• See the dialog example for how to implement the code.

Page 31: Cosc  5/4730

Listener and Summary

• Again much of the code is depreciated for the Activity (3.0+) and then works in the fragment.

• The code shows it for compatibility.– The code has the if statements in activity when

using the fragment– Otherwise the code is identical.

Page 32: Cosc  5/4730

Code

• Setup some variables in onCreate(…) // Get a reference to the preferences, so we can dynamically update the preference screen summary info.mEditTextPreference = (EditTextPreference)getPreferenceScreen().findPreference("textPref"); mListPreference = (ListPreference)getPreferenceScreen().findPreference("list_preference");

• Implement the OnSharedPreferenceChangeListener@Overridepublic void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (key.equals("textPref")) { //where textPref is the key used in the xml. mEditTextPreference.setSummary( "Text is " + sharedPreferences.getString("textPref", "Default")); } else if (key.equals("list_prefrence")) { mListPreference.setSummary("Current value is " + sharedPreferences.getString(key, "Default")); }}

Page 33: Cosc  5/4730

Code (2)

• Setup to read and setup the summary fields in the onResume() and setup the listener.

• Remove the listener in the onPause()

Page 35: Cosc  5/4730

Support library preferences

• Android finally added a v7 and v14 for preferences.– Include the following:

– import android.support.v7.preference.PreferenceFragmentCompat;– import android.support.v7.preference.PreferenceScreen;

– Change to a PereferenceFragmentCompat

– Not everything is working, like the sub screens. Which are supposed to work. The library still have a couple of errors and will hopefully fix soon.

• See the supportpreferencedemo, which is the same as preferencedemo, except it uses the support library.

Page 36: Cosc  5/4730

References• http://

developer.android.com/reference/android/preference/PreferenceActivity.html

• http://developer.android.com/reference/android/preference/PreferenceFragment.html

• http://stackoverflow.com/questions/7112706/preferencefragment-alternative-for-the-android-compatibility-api

• http://android-er.blogspot.com/2012/07/example-of-using-preferencefragment.html

• http://gmariotti.blogspot.com/2013/01/preferenceactivity-preferencefragment.html

• http://www.cs.dartmouth.edu/~campbell/cs65/lecture12/lecture12.html

Page 37: Cosc  5/4730

QA&