Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the Challenge assignment Limited Cheats. UI Fragments and the Fragment Manager In this chapter, you will start building an application named CriminalIntent. CriminalIntent records

Complete the Challenge assignment "Limited Cheats". UI Fragments and the Fragment Manager In this chapter, you will start building an application named CriminalIntent. CriminalIntent records the details of office crimes things like leaving dirty dishes in the break room sink or walking away from an empty shared printer after documents have printed. With CriminalIntent, you can make a record of a crime including a title, a date, and a photo. You can also identify a suspect from your contacts and lodge a complaint via email, Twitter, Facebook, or another app. After documenting and reporting a crime, you can proceed with your work free of resentment and ready to focus on the business at hand. CriminalIntent is a complex app that will take 11 chapters to complete. It will have a list-detail interface: The main screen will display a list of recorded crimes, and users will be able to add new crimes or select an existing crime to view and edit its details (Figure8.1). Figure 8.1 CriminalIntent, a list-detail app The Need for UI Flexibility You might imagine that a list-detail application consists of two activities: one managing the list and the other managing the detail view. Pressing on a crime in the list would start an instance of the detail activity. Pressing the Back button would destroy the detail activity and return you to the list, where you could select another crime. That would work, but what if you wanted more sophisticated presentation and navigation between screens? Consider the possibility of CriminalIntent running on a large device. Some devices have screens large enough to show the list and detail at the same time at least in landscape orientation (Figure8.2). Figure 8.2Ideal list-detail interface for varying screen widths Or imagine a user is viewing a crime and wants to see the next crime in the list. It would be better if they could select a different crime from the list without navigating back to the previous screen first. What these scenarios have in common is UI flexibility: the ability to compose and recompose an activitys view at runtime depending on what the user or the device requires. Activities were not built to provide this flexibility. An activitys views may change at runtime, but the code to control those views must live inside the activity. As a result, activities are tightly coupled to the particular screen being used. Introducing Fragments You can ensure that your app is flexible by moving the apps UI management from the activity to one or more fragments. A fragment is a controller object that an activity can deputize to perform tasks. Most commonly, the task is managing a UI. The UI can be an entire screen or just one part of the screen. A fragment managing a UI is known as a UI fragment. A UI fragment has a view of its own that is inflated from a layout file. The fragments view contains the interesting UI elements that the user wants to see and interact with. Instead of containing the UI, the activitys view can hold a container for the fragment. The fragments view is inserted into the container once it is inflated. In this chapter, the activity will host a single fragment, but an activity can have multiple containers in its view for different fragments. You can use the fragment (or fragments) associated with the activity to compose and recompose the screen as your app and users require. The activitys view technically stays the same throughout its lifetime, and no laws of Android are violated. Lets see how this would work in a list-detail application to display the list and detail together. You would compose the activitys view from a list fragment and a detail fragment. The detail view would show the details of the selected list item. Selecting another item should display a new detail view. This is easy with fragments; the activity will replace the detail fragment with another detail fragment (Figure8.3). No activities need to die for this major view change to happen. Figure 8.3Swapping out a detail fragment Using UI fragments separates the UI of your app into building blocks, which is useful for more than just list-detail applications. Working with individual blocks, it is easy to build tab interfaces, tack on animated sidebars, and more. Additionally, some of the new Android Jetpack APIs, such as the navigation controller, work best with fragments. So using fragments sets you up to integrate nicely with Jetpack APIs. Starting CriminalIntent In this chapter, you are going to start on the detail part of CriminalIntent. Figure8.4

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions