Question

In: Computer Science

What are the various layouts that are created when a developer utilizes the Fragment option in...

What are the various layouts that are created when a developer utilizes the Fragment option in the Android Design Support Library? What are the components of each created layout? How can these layouts be used (provide an example for each layout)?

Solutions

Expert Solution

A Fragment represents a behavior or a portion of user interface in a FragmentActivity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities)

When you add a fragment as a part of your activity layout, it lives in a ViewGroup inside the activity's view hierarchy and the fragment defines its own view layout. You can insert a fragment into your activity layout by declaring the fragment in the activity's layout file, as a <fragment> element, or from your application code by adding it to an existing ViewGroup.

You should design each fragment as a modular and reusable activity component. That is, because each fragment defines its own layout and its own behavior with its own lifecycle callbacks, you can include one fragment in multiple activities, so you should design for reuse and avoid directly manipulating one fragment from another fragment. This is especially important because a modular fragment allows you to change your fragment combinations for different screen sizes. When designing your application to support both tablets and handsets, you can reuse your fragments in different layout configurations to optimize the user experience based on the available screen space. For example, on a handset, it might be necessary to separate fragments to provide a single-pane UI when more than one cannot fit within the same activity.

For example—to continue with the news application example—the application can embed two fragments in Activity A, when running on a tablet-sized device. However, on a handset-sized screen, there's not enough room for both fragments, so Activity A includes only the fragment for the list of articles, and when the user selects an article, it starts Activity B, which includes the second fragment to read the article. Thus, the application supports both tablets and handsets by reusing fragments in different combinations.

To create a fragment, you must create a subclass of Fragment (or an existing subclass of it). The Fragment class has code that looks a lot like an Activity. It contains callback methods similar to an activity, such as onCreate(), onStart(), onPause(), and onStop(). In fact, if you're converting an existing Android application to use fragments, you might simply move code from your activity's callback methods into the respective callback methods of your fragment.

Usually, you should implement at least the following lifecycle methods:

onCreate()

The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.

onCreateView()

The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.

onPause()

The system calls this method as the first indication that the user is leaving the fragment (though it doesn't always mean the fragment is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).

Most applications should implement at least these three methods for every fragment, but there are several other callback methods you should also use to handle various stages of the fragment lifecycle. All the lifecycle callback methods are discussed in more detail in the section about Handling the Fragment Lifecycle.

There are also a few subclasses that you might want to extend, instead of the base Fragment class:

DialogFragment

Displays a floating dialog. Using this class to create a dialog is a good alternative to using the dialog helper methods in the Activity class, because you can incorporate a fragment dialog into the back stack of fragments managed by the activity, allowing the user to return to a dismissed fragment.

ListFragment

Displays a list of items that are managed by an adapter (such as a SimpleCursorAdapter), similar to ListActivity. It provides several methods for managing a list view, such as the onListItemClick() callback to handle click events. (Note that the preferred method for displaying a list is to use RecyclerView instead of ListView. In this case you would need to create a fragment that includes a RecyclerView in its layout. See Create a List with RecyclerView to learn how.)

PreferenceFragmentCompat

Displays a hierarchy of Preference objects as a list. This is used to create a settings screen for your application.


Related Solutions

In excel, use payoffs to show the trading position created when a call option is bought...
In excel, use payoffs to show the trading position created when a call option is bought with strike price X2 = $60 and a put option is sold with strike price X1 = $40. What's the new position when X1 = X2 = $50?
what resources are used when LWP is created and how do they differ when  HWP is created?
what resources are used when LWP is created and how do they differ when  HWP is created?
-what are the methods of evaluating the layouts ?and diffrence between them -what is meant by...
-what are the methods of evaluating the layouts ?and diffrence between them -what is meant by analytical hierarchy process used in systematic layout planning evaluation ?how and when to use it? -what is numerical scoring mehods to evaluate the block lay out in the systematic layout planning ? how and when to use it?
If an asteroid fragment contains carbon, it may burn when entering Earth's atmosphere. What is the...
If an asteroid fragment contains carbon, it may burn when entering Earth's atmosphere. What is the most likely compound to result from this process? Which type of chemical bond would result from this process? Of the types chemical reactions studied in this course, which type would this be? Be sure to fully explain all of your answers.
A software developer has created a new dating app that she thinks users will prefer over...
A software developer has created a new dating app that she thinks users will prefer over current options. She samples 260 millennials, and randomly splits them into 2 evenly-sized separate samples: the New Dating App Group and the Tinder Group. The dependent variable is self-reported satisfaction of the app on a 1-10 scale. Choose the best statistical test for this research design. A. Repeated Measures ANOVA B. Correlation C. Independent measures t-test D. Regression
what role does the symbol table play in compiler when it is created
what role does the symbol table play in compiler when it is created
What is the current federal minimum wage, how is it created, and when was the last...
What is the current federal minimum wage, how is it created, and when was the last time it was changed? Explain how the minimum wage is related to the cost of living, i.e. what is the real minimum wage?
What is the debt ceiling? When and why was it created? Discuss the pros and cons...
What is the debt ceiling? When and why was it created? Discuss the pros and cons for raising the debt ceiling.
What is the World Trade Organization (WTO? When was the WTO created and how is it...
What is the World Trade Organization (WTO? When was the WTO created and how is it related to the GATT? Roughly how many countries are members of the WTO and what are the broad issues related to agriculture that are being debated in WTO negotiations? Why does the world need organizations such as the World Trade Organization (WTO)? In other words, what is the role of the World Trade Organization?
A bull spread strategy is created by buying a European call option on a stock with...
A bull spread strategy is created by buying a European call option on a stock with a certain strike price and selling a European call option on the same stock with a higher strike price. Both options have the same expiration date. Suppose that an investor buys for £3 a 3-month European call with a strike price of £29 and sells for £1 a 3-month European call with a strike price of £34. What is the total payoff of this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT