CHAPTER 4 Fragments ActionBar Menus • Explore how to build applications that use an ActionBar and Fragments • Understand the Fragment lifecycle • Learn to configure the ActionBar • Implement Fragments with Responsive Design Techniques • Explore animation in Fragment Transactions • Experiment with Fragments, ListViews, and ArrayAdapters 4.1 Fragmentation and Android Fragments • There are many shapes and screen sizes and different performance characteristics • Multiple versions of Android are being used • This translates into a wider audience to build applications for • When designing and coding layouts, there are several factors to consider, such as spacing, positioning, size, and the grouping of user interface controls • Layouts must be compatible with many physical screens • A Fragment is modeled as a subdivision of an Activity • Fragment is integrated into an Activity, and needs an Activity to run • The advantage of building applications that use Fragments is their ability to easily present a consistently welldesigned user interface • Fragments are particularly helpful in adapting a user experience across a wide range of devices. 4.2 The Fragment Lifecycle • Fragments are associated with an activity and can be created, added or removed while the activity is running • Similar to an Activity, a Fragment has its own lifecycle, as well as its own user interface • The Fragment’s lifecycle is connected to the activity that owns it • Each fragment has its own callback methods in the standard Activity lifecycle. Callbacks used in the lifecycle of a Fragment. • onCreateView() – bring a fragment to resumed state, interacting with user • onInflate() – called every time fragment is inflated • onActivityCreated() – called when fragment’s activity has been created and this fragment’s view hierarchy is instantianted • onAttach() – called after the fragment has been attached to an activity • onDestroyView() – informs the fragment that its view is being destroyed so that it can clean up any associated resources. Called after onStop() and before onDestroy() • onDetach() – called when fragment is no longer attached to its activity, after on Destroy(). This is the final call before the fragment object is released to garbage collector 4.3 Action Bar • The action bar provides information and displays control elements to the user. • In a basic configuration, the action bar displays the application icon and a title • The title often identifies the running activity • Users are given an indication of where they are and a consistent identity from which to recognize the application Action bar features: • Application Icon • Action Items • Action overflow 4.4 ActionBar Configurations • Control elements that appear directly on the action bar as an icon and/or text are known as action buttons • When an Activity starts, its associated layout is inflated on the screen and the action bar is populated with action buttons • The activity's onCreateOptionsMenu() method is responsible for inflating a menu hierarchy from a specified XML resource file 4.4.1 Overflow on the Action Bar • Narrow devices can often require the use of the overflow button • When creating layouts on devices with a narrow screen, use ifRoom to request that an item appear in the action bar • Allow the system to move elements into the overflow when there is not enough room 4.4.2 Adding an Action View • An action view is simply a widget that appears in the action bar as a substitute for an action button • An action view provides quick access to heavily used actions • Consider a collapsible search view widget. A search action view can be added as an embedded search view widget in the action bar • To declare an action view, the actionLayout or actionViewClass attribute can be added to an item to specify either a layout resource or a widget class 4.5 Responsive Design with Fragments • Responsive design works across different screens sizes • Responsive design techniques should also be applied to Android applications for solving interactive design and layout problems • Responsive design is used for heavy data driven content. • Adaptive design is primarily used for the rearrangement of fixed user interface elements in an application • Responsive design revolves around a master/detail flow interface design pattern • The user is provided with a list of items • Upon selecting one of the items, additional information relating to that item is then presented to the user • This design concept is called responsive in the sense that list and detail panels of the app can change based on the width of the device • On a large tablet-sized Android device, the screen is large enough to display both panels • The master list can appear as a narrow vertical panel along the left hand edge of the screen while the remainder of the screen can display the detail panel • This arrangement is referred to as twopane mode. • Responsive design makes use of Fragments • A user interface can be divided into multiple panes using Fragments and reused in more than one screen of an application, as shown in the following figure • Fragments can be combined or separated on a device • When building an application that follows a similar master/detail design pattern, the application often needs a set of Java and XML layout resource files 4.6 Animation in Fragment Transactions • Transition animations can be applied directly to fragments that are entering and exiting a transaction • The FragmentManager provides the structure that handles transactions between fragments • A transaction refers to the sequence of steps that add, replace, or remove fragments. • Operations performed by the FragmentManager will occur inside a transaction 4.7 ListViews and Dynamic Data • A ListView is similar to a ScrollView • A ScrollView is an extension of the FrameLayout, and is suitable for holding a single control element • It provides the user with the scroll mechanism to reveal more content than can be displayed on the screen at once • A LinearLayout, containing multiple View items, can be placed within a ScrollView • A ListView is a specialized control that is optimized for displaying long lists of items • When the data content for the layout is dynamic or not pre-determined, it is possible to use a layout that subclasses an AdapterView to populate the layout with views at runtime 4.8 Handling Click Events in a ListView • A ListView might be populated with items that need to respond to a click event • You can respond to click events on an item in an AdapterView by implementing the AdapterView.OnItemClickListener interface • The onItemClick () callback method will always be invoked when an item in the AdapterView has been clicked.