14
Objectives
In this chapter you’ll:
■
Use WeatherBug® web
services to get the current
conditions and five-day
forecast for a specified city
and process that data using
an Android 3.x
JsonReader.
■
Use various types of
Fragments to create
reusable components and
make better use of the screen
real estate in a tablet app.
■
Implement tabbed navigation
using the Android 3.x
ActionBar.
■
Create a companion app
widget that can be installed
on the user’s home screen.
■
Broadcast changes of the
app’s preferred city to the
companion app widget.
Weather Viewer App
2
Chapter 14
Weather Viewer App
Self-Review Exercises
14.1
Fill in the blanks in each of the following statements:
.
a) A ListFragment is a Fragment containing a
ANS: ListView.
allows
b) A FragmentTransaction (package android.app) obtained from the
the Activity to add, remove and transition between Fragments.
ANS: FragmentManager.
c) We extend class AppWidgetProvider (package android.appwidget), a subclass of
(package android.content), to create an app widget and allow it to receive
notifications from the system when the app widget is enabled, disabled, deleted or updated.
ANS: BroadcastReceiver
d) You can force an item to appear in the ActionBar by using the always value of attribute
but you risk overlapping menu items by doing so.
ANS: android:showAsAction.
14.2
State whether each of the following is true or false. If false, explain why.
a) (True/False) Fragments were introduced in Android 3.x and cannot be used with earlier
versions of Android.
ANS: False. Though fragments were introduced in Android 3.x, there’s a compatibility
package that enables you to use them with earlier versions of Android.
b) (True/False) The action bar can display the app’s options menu, navigation elements
(such as tabbed navigation) and other interactive GUI components.
ANS: True.
c) (True/False) Unlike activities, services need not be registered in the manifest.
ANS: False. Like activities, all services must be registered in the manifest; otherwise, they
cannot be executed.
d) (True/False) JSON (JavaScript Object Notation)—a simple way to represent JavaScript
objects as numbers—is an alternative to XML for passing data between the client and
the server.
ANS: False. JSON (JavaScript Object Notation)—a simple way to represent JavaScript objects as strings—is an alternative to XML for passing data between the client and the
server.
e) (True/False) Arrays are represented in JSON with curly braces in the following format:
{ value1, value2, value3 }
ANS: False. Arrays are represented in JSON with square brackets.
f) (True/False) Class JsonReader provides methods for reading booleans, doubles, ints,
longs and Strings.
ANS: True.
g) (True/False) A PendingIntent cannot be passed across processes.
ANS: False. A PendingIntent can be passed across processes.
h) (True/False) Use Fragments to create reusable components and make better use of the
screen real estate in a tablet app.
ANS: True.
Exercises
14.3
Fill in the blanks in each of the following statements:
Exercises
3
a) An Activity’s
(package android.content.res) can be used to determine
the current orientation.
ANS: Configuration.
(package android.util) to read JSON objects.
b) Use a(n)
ANS: JsonReader.
c) The attribute android:showAsAction defines how a menu item should appear in the Acspecifies that this item should be visible in the ActiontionBar. The value
Bar if there’s room to lay it out completely.
ANS: ifRoom.
d) We get the name from the next name–value pair in a JSON object by calling JsonReader’s
method.
ANS: nextName.
e) You use a FragmentManager to manage the Fragments and a
to add, remove
and transition between Fragments.
ANS: FragmentTransaction.
14.4
State whether each of the following is true or false. If false, explain why.
a) (True/False) Fragments are a key feature of Android 3.x.
ANS: True.
b) (True/False) The base class of all fragments is BaseFragment (package android.app).
ANS: False. The base class of all fragments is Fragment (package android.app).
c) (True/False) Like an Activity, each Fragment has a life cycle.
ANS: True.
d) (True/False) Fragments can be executed independently of a parent Activity.
ANS: False. Fragments must be hosted in a parent Activity—they cannot be executed independently.
e) (True/False) It’s common practice to allow a user to launch an app by touching the app’s
companion widget on the device’s home screen.
ANS: True.
f) (True/False) Each object in JSON is represented as a list of property names and values
contained in curly braces, in the following format:
{ "propertyName1" : value1, "propertyName2'": value2 }
ANS: True.
g) (True/False) Each value in a JSON array can be a string, a number, a JSON representation of an object, true, false or null.
ANS: True.
h) (True/False) When updating an app widget from an AppWidgetProvider, you update
the app widget’s Views directly.
ANS: False. When updating an app widget from an AppWidgetProvider, you do not update
the app widget’s Views directly. You use an object of type RemoteViews.
© Copyright 2026 Paperzz