Android: “Dynamic” data and Preferences data.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Cosc 5/4730 Android Services. What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what.
Programming with Android: Activities
Android 02: Activities David Meredith
All About Android Introduction to Android 1. Creating a New App “These aren’t the droids we’re looking for.” Obi-wan Kenobi 1. Bring up Eclipse. 2. Click.
The Android Activity Lifecycle. Slide 2 Introduction Working with the Android logging system Rotation and multiple layouts Understanding the Android activity.
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Android Fragments.
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first.
Cosc 4730 Phone Programming in Java An Introduction.
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
Android Development (Basics)
Data Storage: Part 1 (Preferences)
Android Fragments A very brief introduction Android Fragments1.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
Chapter 4 Code Editor Goals and Objectives Program more efficiently? How can you speed up your development process? Do you want to learn useful shortcuts.
ANDROID UI – FRAGMENTS. Fragment  An activity is a container for views  When you have a larger screen device than a phone –like a tablet it can look.
Android Mobile computing for the rest of us.* *Prepare to be sued by Apple.
Lecture Series on Android Programming Lecturer: Prof.Luqun Li Teaching Assistants: Fengyou Sun, Haijun Yang, Ting.
Tip Calculator App Building an Android App with Java © by Pearson Education, Inc. All Rights Reserved.
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Cosc 5/4730 Android Content Providers and Intents.
Cosc 5/4730 Broadcast Receiver. Broadcast receiver A broadcast receiver (short receiver) – is an Android component which allows you to register for system.
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
Android Programming-Activity Lecture 4. Activity Inside java folder Public class MainActivity extends ActionBarActivity(Ctrl + Click will give you the.
Understand applications and their components activity service broadcast receiver content provider intent AndroidManifest.xml.
Data persistence How to save data using SharedPreferences, Files, and SQLite database 1Data persistence.
Android – Fragments L. Grewe.
Cosc 5/4730 Android Communications Intents, callbacks, and setters.
Cosc 4730 Android Fragments. Fragments You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own.
Android Boot Camp Demo Application – Part 1. Development Environment Set Up Download and install Java Development Kit (JDK) Download and unzip Android.
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
Persistence Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Video Games list lab 6  At the end of this lab you will be expected to know:  What Views, View Groups, Layouts, and Widgets are and how they relate to.
Mobile Programming Lecture 7 Dialogs, Menus, and SharedPreferences.
Android Application Lifecycle and Menus
Written by: Dr. JJ Shepherd
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
Cosc 5/4735 YouTube API. YouTube The YouTube Android Player API enables you to incorporate video playback functionality into your Android applications.
Cosc 5/4735 Voice Actions Voice Interactions (API 23+)
CHAPTER 9 File Storage Shared Preferences SQLite.
Mobile Programming Lecture 4 Resources, Selection, Activities, Intents.
Lecture 5: Location Topics: Google Play Services, Location API Date: Feb 16, 2016.
School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,
Android Fragments. Slide 2 Lecture Overview Getting resources and configuration information Conceptualizing the Back Stack Introduction to fragments.
The Flag Quiz app tests your ability to correctly identify 10 flags from various countries and territories.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Putting together the AndroidManifest.xml file Creating multiple.
Cosc 4735 Nougat API 24+ additions.
Introduction to android
Google VR (gvr) CardBoard and DayDream With OpenGL
Activity and Fragment.
Android – Event Handling
Android Studio, Android System Basics and Git
Activities, Fragments, and Events
Activities and Intents
Android Activities An application can have one or more activities, where Each activity Represents a screen that an app present to its user Extends the.
The Android Activity Lifecycle
Android 15: Settings Kirk Scott.
Android – Fragments L. Grewe.
ANDROID UI – FRAGMENTS UNIT II.
Android: Preferences and user settings data
Activities and Intents
SE4S701 Mobile Application Development
Cosc 4730 An Introduction.
Activities and Fragments
Implication of Orientation Changes
Preference Activity class
Activities, Fragments, and Intents
Presentation transcript:

Android: “Dynamic” data and Preferences data. Cosc 5/4730 Android: “Dynamic” data and Preferences 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 More permanent data.

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.

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 http://developer.android.com/reference/android/app/Activity.html

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.

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); }

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); }

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

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

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();

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()

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); }

References http://developer.android.com/reference/android/app/Activity.html http://www.eigo.co.uk/Managing-State-in-an-Android-Activity.aspx http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state

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”

Preferences

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.

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.

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.

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 }

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.

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>

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

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

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" />

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>

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), …

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.

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.

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.

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.

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.

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")); }

Code (2) Setup to read and setup the summary fields in the onResume() and setup the listener. Remove the listener in the onPause()

preferences xml. Only covered a subset of the preferences xml, including some interfaces. see http://developer.android.com/reference/android/preference/Preference.html for more information. While out some what out of date, this is very helpful with for changing the android:summary http://stackoverflow.com/questions/531427/how-do-i-display-the-current-value-of-an-android-preference-in-the-preference-su

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.

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

Q A &