Android How To Program 3rd Edition By Deitel & Deitel – Test Bank
Address Book App
8.2 Test-Driving the Address Book App
8.3 Technologies Overview
8.3.1 Displaying Fragments with FragmentTransactions
1. Which of the following statements is false?
a. You can declare each Fragment in an Activity’s layout or, for a Dialog-Fragment, call its show method to create it.
b. An app can use multiple activities to host an app’s Fragments on a phone device.
c. You can use only one Activity to host all of an app’s Fragments.
d. On a phone-sized device, you’ll typically display several Fragments at a time.
Ans: d. On a phone-sized device, you’ll typically display several Fragments at a time. [Actually, on a phone-sized device, you’ll typically display one Fragment at a time.]
2. You’ll use Android’s ________—a data structure that stores Fragments in last-in-first-out (LIFO) order-—to provide automatic support for the Android system bar’s back button and to allow the app to remove Fragments in the re-verse order from which they were added.
Ans: Fragment back stack
8.3.2 Communicating Data Between a Fragment and a Host Activity
3. (True/False) To communicate data between Fragments and a host Activity or the Activity’s other Fragments, it’s considered best practice to do so through the root Fragment.
Ans: False. Actually, it’s considered best practice to do so through the host Activity—this makes the Fragments more reusable, because they do not refer to one another directly.
4. Typically, each Fragment defines an interface of ________ that are imple-mented in the host Activity.
Ans: callback methods
8.3.3 Method onSaveInstanceState
5. Method ________ is called by the system when the configuration of the de-vice changes during the app’s execution—for example, when the user rotates the device or slides out a keyboard on a device with a hard keyboard. This method can be used to save state information that you’d like to restore when the app’s onCreate method is called as part of the configuration change.
6. (True/False) When an app is simply placed into the background, perhaps so the user can answer a phone call or when the user starts another app, the app must save the contents of its GUI components for when the app is brought back to the foreground (provided that the system does not kill the app).
Ans: False. Actually, when an app is simply placed into the background, perhaps so the user can answer a phone call or when the user starts another app, the app’s GUI components will automatically save their contents for when the app is brought back to the foreground (provided that the system does not kill the app).
8.3.4 Defining Styles and Applying Them to GUI Components
7. (True/False) You can define common GUI component attribute–value pairs as style resources. You can then apply the styles to all components that share those values by using the style attribute. Any subsequent changes you make to a style are automatically applied to all GUI components.
Ans: False. Actually, any subsequent changes you make to a style are au-tomatically applied to all GUI components that use the style.
8.3.5 Specifying a Background for a TextView
8. (True/False) By default TextViews do not have a border.
8.3.6 Extending Class ListFragment to Create a Fragment That Contains a ListView
9. (True/False) When a Fragment’s primary task is to display a scrollable list of items, you can extend class ListFragment. A ListFragment uses a ListView as its default layout.
10. (True/False) You can use a CursorAdapter (package android-.widget) to display the results of a database query in a ListView.
8.3.7 Manipulating a SQLite Database
11. SQLite is one of the world’s most widely deployed database engines. Data-base queries are performed with Structured Query Language (SQL) and query results are managed via a ________ (package android-.database).
8.3.8 Performing Database Operations Outside the GUI Thread with AsyncTasks
12. (True/False) You should perform long-running operations or operations that block execution until they complete (e.g., file and database access) in the GUI thread.
Ans: False. Actually, you should perform long-running operations or opera-tions that block execution until they complete (e.g., file and database access) outside the GUI thread. This helps maintain application responsiveness and avoid Activity Not Responding (ANR) dialogs that appear when Android thinks the GUI is not responsive.
13. When we need a database operation’s results in the GUI thread, we’ll use a subclass of ________ (package android.os) to perform the operation in one thread and receive the results in the GUI thread.
8.4 Building the GUI and Resource Files
8.4.1 Creating the Project
8.4.2 Creating the App’s Classes
1. Why would you double click strings.xml in the res/values folder?
Ans: To display the resource editor for creating String resources.
2. (True/False) Like other resources, style resources are placed in the app’s res/values folder. When you create a project, the IDE creates a styles.xml file containing predefined styles. Each new style you create specifies a name that’s used to apply that style to GUI components and one or more items spec-ifying property values to apply.
3. (True/False) You must define Drawables in the project’s drawable folders for all device sizes and resolutions to be used with your app.
Ans: False. If a Drawable is defined in only one of the project’s drawable folders, Android will use that Drawable on all device sizes and resolutions.
8.4.6 MainActivity’s Layout: activity_main.xml
8.4.7 DetailsFragment’s Layout: fragment_details.xml
4. A ________ is a ViewGroup that can contain other Views (like a layout) and that lets users scroll through content too large to display on the screen.
8.4.8 AddEditFragment’s Layout: fragment_add_edit.xml
5. Each EditText specifies the Input Type and IME Options properties. For devic-es that display a soft keyboard, the Input Type specifies ________ when the user touches the corresponding EditText.
Ans: which keyboard to display
6. Which of the following EditTexts’s Input Type property values might be used to ensure that state abbreviations are displayed in capital letters?
Ans: c. textPostalAddress|textCapCharacters
8.4.9 Defining the Fragments’ Menus
7. (True/False) Each menu item’s Order in category values determines the order in which the menu items appear on the action bar.
8.5 MainActivity Class
1. (True/False) Class MainActivity manages the app’s fragments and coordi-nates the interactions between them. On phones, MainActivity displays one master Fragment and one detail Fragment at a time.
Ans: False. On phones, MainActivity displays one Fragment at a time.
2. (True/False) You can configure a Fragment to be retained across configuration changes, such as when the user rotates the device.
3. If the resource ID ________ exists in MainActivity’s layout, then the app is running on a phone.
4. Call the FragmentManager’s ________ method to remove the top Fragment on the back stack.
5. You can pass arguments to a Fragment by placing them in a ________ of key–value pairs.
6. Call FragmentTransaction method ________ to push a Fragment onto the back stack. This allows the user to touch the back button to pop the Fragment from the back stack and allows MainActivity to programmatically pop the Fragment from the back stack.
8.6 ContactListFragment Class
1. You call Fragment method setRetainInstance with the argument true to indicate that a Fragment should be ________.
Ans: retained rather than recreated when the host Activity is re-created on a configuration change (e.g., when the user rotates the device)
2. You call ListView method ________ to indicate that only one item can be selected at a time.
3. Which of the following statements is false?
a. To display the Cursor’s results in a ListView we create a new Cur-sorAdapter object which exposes the Cursor’s data in a manner that can be used by a ListView.
b. SimpleCursorAdapter is a subclass of CursorAdapter that’s designed to simplify mapping Cursor columns directly to TextViews or ImagesViews de-fined in your XML layouts.
c. You must use standard layout resources for ListView items.
d. You call inherited ListActivity method setListAdapter to bind a ListView to a CursorAdapter, so that the ListView can display the data.
Ans: c. You must use standard layout resources for ListView items. [Actu-ally, you can create your own layout resources for ListView items.]
4. (True/False) Each AsyncTask can be executed many times.
Ans: False. Each AsyncTask can be executed only once. You must create a new instance to execute an AsyncTask again.
5. (True/False) When you call the AsyncTask’s execute method, doInBack-ground performs the task in the GUI thread.
Ans: False. Actually, doInBackground performs the task in a separate thread.
6. (True/False) Fragment lifecycle method onStop is called after onPause when the Fragment is no longer visible to the user.
7. (True/False)You call CursorAdapter method changeCursor with the argu-ment true to remove the Cursor from the CursorAdapter.
Ans: False. Actually, you call CursorAdapter method changeCursor with the argument null to remove the Cursor from the CursorAdapter.
8.7 AddEditFragment Class
1. Call Fragment method getArguments to get the ________ of arguments (if any).
8.8 DetailsFragment Class
1. (True/False) Fragment method onSaveInstanceState executes when the configuration of the device changes during the app’s execution—for example, when the user rotates the device or slides out a keyboard on a device with a hard keyboard.
2. Method onOptionsItemSelected uses the selected MenuItem’s ________ to determine which one was selected.
Ans: resource ID
3. Which of the following statements is false?
a. Cursor method moveToFirst can be used to move the Cursor to the first row in the result set.
b. It’s considered good practice to ensure that Cursor method moveToFirst returns true before attempting to get data from the Cursor.
c. Class Cursor provides method getColumnIndexOrThrow if you prefer to get an exception when the specified column name does not exist.
d. Android automatically releases resources like database connections when they are not being used so that other activities can use the resources.
Ans: d. Android automatically releases resources like database connections when they are not being used so that other activities can use the resources. [Actually, it’s good practice to for your app to explicitly release resources like database connections when they are not being used so that other activi-ties can use the resources.]
8.9 DatabaseConnector Utility Class
4. (True/False) Database names must be unique across apps.
Ans: False. Actually, database names must be unique within a specific app but need not be unique across apps.
5. (True/False) Once a database is opened successfully, it will be cached by the operating system to improve the performance of future database interactions.
6. (True/False) SQLite does not support inserting a completely empty row into a database table.
Ans: True. But there is a workaround: Instead of making it illegal to pass an empty ContentValues to SQLiteDatabase’s insert method, the null-ColumnHack parameter, which is the method’s second parameter) is used to identify a column that accepts NULL values.
7. In the context of using SqLiteDatabase’s query method the Cursor re-turned by method query contains all the table rows that match the method’s arguments—the so-called ________.
Ans: result set
8. If you supply a newer version number than the database version currently stored on the device, the DatabaseOpenHelper’s onUpgrade method will be called to ________.
Ans: upgrade the database to the new version (perhaps to add tables or to add columns to an existing table)
9. (True/False) Class SQLite-OpenHelper also provides the onDowngrade method that can be used to downgrade a database when the currently stored version has a higher version number than the one requested in the call to class SQLite-OpenHelper’s constructor.
10. Why might you use downgrading on a SQLite database?
Ans: Downgrading might be used to revert the database back to a prior ver-sion with fewer columns in a table or fewer tables in the database—perhaps to fix a bug in your app.