Download presentation
Published byAndrew Washington Modified over 9 years ago
1
Android: “Dynamic” data and Preferences data.
Cosc 5/4730 Android: “Dynamic” data and Preferences data.
2
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.
3
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.
4
Another note. Configuration Changes See Configuration Changes on
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
5
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.
6
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); }
7
Restoring data. Via OnCreate(Bundle) if (savedInstanceState != null) {
//There is saved data d1 = savedInstanceState.getString("d1"); if (d1 != null) { //and d1 key exists t1.setText(d1); }
8
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
9
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
10
Storing In OnPause() Super.onPause(); //must be called
Get an spot to start the preferences and edit them. SharedPreferences preferences = getPreferences(MODE_PRIVATE); OR SharedPreferences 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();
11
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()
12
getPrefs() void getprefs() { Get the between instance stored values
SharedPreferences preferences = getPreferences(MODE_PRIVATE); OR SharedPreferences 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); }
13
References
14
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”
15
Preferences
16
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.
17
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.
18
Preferences activity The activity itself is very easy.
Extend the prefrenceActivity and override the onCreate The preferences are kept in xml document. Use the addPreferenceFromResources to use the xml file.
19
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 }
20
PreferenceFragment The code is basically the same, but in a fragment.
public class PrefFrag extends PreferenceFragment { @Override public 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.
21
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> </PreferenceScreen>
22
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
23
EditTextPreference <EditTextPreference android:key="edittext_preference" android:title="EditText Preference“ android:defaultValue="" android:summary="Example of EditText Dialog" android:dialogTitle="Enter your Name" />
24
ListPreference <ListPreference />
android:dialogTitle="list preference example“ android:summary="List preference demo" android:key="list_preference" android:title="list perference" />
25
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>
26
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), …
27
preference example prefs declared like on last slide
boolean 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.
28
Compatibility Since there is no support.v4 fragment, you need to add a little extra code in the fragmentActivity public 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.
29
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.
30
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.
31
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.
32
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 @Override public 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")); }
33
Code (2) Setup to read and setup the summary fields in the onResume() and setup the listener. Remove the listener in the onPause()
34
preferences xml. Only covered a subset of the preferences xml, including some interfaces. see for more information. While out some what out of date, this is very helpful with for changing the android:summary
35
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.
36
References
37
Q A &
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.