Welcome to Part 3 of this review of the Pluralsight course Improving User Interface Design with Android Fragments by Jim Wilson.
Jim has over 30 years of software engineering experience, with the past 15 years heavily focused on creating mobile device and location-based solutions. He has co-founded multiple software-related startups and has served in a consulting role at several more.
After nearly a decade as a Microsoft Device Application Development MVP, Jim now focuses on developing Android and iOS device applications. Jim’s passion is mentoring software developers. He blogs at hedgehogjim.wordpress.com.
Improving User Interface Design with Android Fragments is the 8th course in the Pluralsight learning path for Android, and this month I am reviewing every course in this learning path.
Also in this series:
Fragment Lifecycle and Specialization
Fragment lifecycle
Jim says as we start doing more sophisticated things with fragments, an understanding of the fragment lifecycle becomes essential.
He explains the setup lifecycle first:
- onCreate
- onStart
- onResume
Each Activity callback is called before the Fragment callback of the same name.
He introduces the fragment specific setup and display callbacks:
- onAttach
- onCreateView
- onActivityCreated
- onAttachFragment
We see an illustration of the setup/display lifecycle sequence.
- Activity onCreate
- Fragment onAttach
- Activity onAttachFragment
- Fragment onCreate
- Fragment onCreateView
- Fragment onActivityCreated
- Activity onStart
- Fragment onStart
- Activity onResume
- Fragment onResume
Jim introduces the hide/teardown callbacks as well:
- onPause
- onStop
- onDestroy
Each Activity callback is called after the Fragment callback of the same name.
The fragment-specific teardown callbacks are:
- onDestroyView
- onDetach
Types of Fragments
Jim introduces the following:
- Fragment class
- ListFragment class
- WebViewFragment
- DialogFragment class
The DialogFragment class allows us to use dialogs and maintain a consistent programming model with fragments.
ListFragment
This is used to display a user-selectable list.
Some of the responsibilities of this class are:
- Automatically displaying data contains in a ListAdapter
- Specifying the data source
For specifying the layout of list rows, there’s a predefined list which you can find at https://developer.android.com/reference/android/R.layout.html
An example is android.R.layout.simple_list_item_activated_1
Demo: ListFragment
We see a new project called “FragmentTypes” which we’ll be using throughout this module.
Jim creates a new class MyListFragment which inherits ListFragment and overrides onActivityCreated.
He warns us not to access the ListAdapter directly. Instead we should go through the ListFragment class.
To associate with a ListAdapter we use setListAdapter.
ListFragment customization
We don’t have to customize the layout, but we may want to.
The ListFragment has a default view but we can change it by creating a layout resource.
We inflate the layout in onCreateView as a regular Fragment-derived class.
Demo: ListFragment customization
We start by looking at the demo we created earlier with the default view. It’s white text on a black background.
We change this to have a green background by using R.layout.mycustomListLayout.
Next we create an empty list to see how that renders. This has a red background.
WebViewFragment
This allows us to embed web content within a Fragment. It’s a web view wrapped inside a fragment.
DialogFragment
This makes dialog handling similar to other Fragments.
Jim says it gets rid of a lot of the special programming that otherwise needs to go into dialogs.
Instead of using the regular fragment methods (Add, Replace etc.) there’s a special method called show, and this takes the layout and displays it as a dialog.
There are actually two show methods: one that takes a FragmentManager and one that takes a FragmentTransaction parameter.
Jim explains that we can control the appearance of our dialog with styles and themes. We see some style examples:
- STYLE_NORMAL
- STYLE_NO_TITLE
- STYLE_NO_FRAME
- STYLE_NO_INPUT
A couple of theme examples are
- android.R.style.Theme_Holo
- android.R.style.Theme_Holo_Light_Dialog
Demo: DialogFragment
Jim begins by showing us mydialogfragment.xml and this is a LinearLayout with a “Do you really want to?” TextView and child LinearLayout containing the Yes and No buttons.
We create MyDialogFragment.java and override onCreateView.
We implement the View.OnClickListener interface and its onClick method, getting the view id and handling the Yes and No click conditions.
We close the Dialog with the dismiss method.
Jim shows us how to link the buttons up in onCreateView with the findViewById, setOnClickListener and requestFocus methods.
Jim asks how do we actually display this?
We do this in MyActivity.java, and we see how to build out the onMenuDialog method, using the FragmentManager as an argument in our MyDialogFragment show method.
We see this works, but with no title displayed. We’ll fix later in this module.
Jim demonstrates the effect of using the show method with the FragmentTransaction instead of the FragmentManager. He warns against calling commit here because this is already done inside the show method.
The Dialog within the DialogFragment
Jim explains that during the construction process of DialogFragment, which is accessible using the getDialog method.
Most of the Dialog class behaviours are available to us, and common methods include setTitle and setCanceledOnTouchOutside.
Demo: The Dialog within the DialogFragment
In MyDialogFragment.java we extend the onCreateView method, getting the dialog and setting the title.
We also see that the dialog is not modal. To make it modal we call setCanceledOnTouchOutside with false as the argument.
Wrapping standard Dialogs in a DialogFragment
We learn that we can wrap existing Dialog classes in a DialogFragment by overriding onCreateDialog.
Jim says the most common use for this is with the built-in AlertDialog class. This is a way of creating very consistent looks for the Dialog across multiple applications.
Jim explains how we use the Builder class inside onCreateDialog.
Demo: Wrapping standard Dialogs in a DialogFragment
In this demonstration we implement the DialogInterface and its onClick method.
We call setPositiveButton and setNegativeButton on our Builder class.
In onClick we add a switch statement with cases for the positive and negative events.
Finally we show the AlertDialog fragment in MyActivity.java.
When we run it up we see the dialog “Do you wanna give it a try?” with options “Stay safe” and “Go for it”.
Using DialogFragments as a standard Fragment
We can take one layout and use it as a dialog or as a regular fragment.
If we use it as a fragment, we get the add, replace and other useful methods available to us.
Jim gives the example of having a dialog occupy the full screen on a small screen device, but just the middle of the screen on larger devices.
There is the caveat of needing to use an onCreateView based layout. We can’t wrap a dialog class because onCreateDialog never gets called and so there are no dialog characteristics for it.
Demo: Using DialogFragments as a standard Fragment
We start with a basic Dialog with “This the Title” and “do you really want to?”.
We enhance it so that we can also display it as a regular fragment, with minimal code changes.
We don’t touch MyDialogFragment.java at all. We just implement the onMenuDialogAdd method in MyActivity.java.
In the app when we click the “Dialog (Add)” option, we see the app crashes!
Jim explains this is because onCreateView uses dialog specific capabilities. We see how to fix this.