CS 185C/286 - Lecture 5

Cay S. Horstmann
Lecture 5 Quiz 1
Put your answer into Piazza.
What is NOT true about RSS parser code example in Android Recipes Section 3.8?
- If the author had used the same trick as we did in the homework, the
startElement
method would have been shorter.
- The
channel
element must be handled in the startElement
method.
- The
title
element must be handled in the endElement
method.
- The
inItem
flag is necessary.
Lecture 5 Quiz 2
What is NOT true about the XML pull parser implementation of the RSS parser code?
- It is longer than the SAX implementation.
- Its structure is more aligned with the structure of the data to be parsed.
- Unlike the RSS parser, the pull parser checks that the document is valid.
- The statement
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
is necessary.
Today's Lecture/Lab
Menus
- In the olden days, Android phones had a dedicated menu key.
- It was pretty nice for those people who understood menus. Push the key, and you get a menu of options that make sense on the current screen.
- Apparently, that's too complicated. Nowadays, the most common actions should go in the action bar.
- Not enough room? Put the rest into the overflow.
- Legacy menus also end up in an overflow at the bottom of the screen (next to Back, Home, Recent apps)
- See here for the exact compatibility rules.
Programming Menus
- Add menu items to resource:
<item android:id="@+id/new_game"
android:icon="@drawable/ic_new_game"
android:title="@string/menu_new_game"
android:showAsAction="ifRoom"/>
- Override
onCreateOptionsMenu
and “inflate” menu (i.e. read from XML):
@Override public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}
- React to menu selection:
@Override public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_new_game) { ...; return true; }
else if (...) ...
else return false;
}
Contextual Menus
- The menus we discussed are called "options menus"
- Show up in the action bar or, with older Androids, somewhere when the menu button is pushed
- Menu items should apply to the current activity
- Contextual menu applies to single item within activity
- Commonly activated through long press
- Can show up as floating menus or action mode

- Also have more specialized
PopupMenu
—see your reading assignment
Storage Options
- Preferences, similar to Windows registry
- Internal storage: private file system
- External Storage: shared file system (ex. SD card)
- SQLite database
- Network connection: save on server
File Storage
SQLite
- Widely used open source project
- Embedded database: runs in-process, uses normal file system
- Uses standard SQL commands. (Don't know SQL? Programming Android Ch. 9 has a quick summary.)
- Use when you have more data than you want to hold in memory
- Use for long-term storage
- You need to bridge O/R mismatch
- Can debug database with
sqlite3
ADK tool
Server Communication
Default/AndroidHttpClient
(don't use) and HttpUrlConnection
(standard Java)
- See Android Recipes Section 3.6
- Remember—all this needs to happen in a separate thread
- And someone needs to write the server ☺
Reading Before Next Class

Lab

- Bring your laptop to the lab
- You will work with a buddy
- One of you writes code, the other types up answers
- Switch roles each lab
- Submit lab work to git
Menu Items
- Continue with the code from the solution to homework 2.
- Add a menu with menu items to show correct responses, and one to hide them. What's the XML?
- Add a menu handler. Set or clear a Boolean flag
showCorrectResponses
. What's the code?
- Now you need to actually make it happen. Somewhere, where you populate the list, respect the flag. Where is that?
- How do you get the list to repopulate when the flag changes?
Saving State
- We want to save the answers of the user so far.
- Quick hack: Just serialize the Quiz object.
- In which lifecycle method should that be done?
- Write the code for saving the object to
quiz.ser
in the cache directory. What is your code? (NB. Also remember to make the Quiz
class implement Serializable
.)
- To test this, click on the Back button until you leave the app. Where can you find the file? How big is it? (Hint: Run
adb shell
from the command line and look inside /data/data
.)
- Now we need to deserialize when the app starts again. If it fails (e.g. on the first try), read from the web. In which method?
- What is your code?
- First check that your app comes up again when you relaunch it from the app screen. How can you tell?
- Now pause the app again and clear the cache. How did you do that?
- What happens when you relaunch your app?