Mobile Applications (Android Programming) Semester II 2016 - 2017 Dr. Saman Mirza Abdullah
Class Objective The objective of this class is: To learn and understand Fragments. To learn the functions that needed to build fragments. Mobile Application - Ishik
Fragments Fragment is an Android programming aspect that represents a portion of the user interface of what a user sees on the application window. The Android documentation describes Fragment as a portion of user interface in an Activity Object. This means an Activity object can be composed of one or more fragments, each having its own user interface definition. Developers can combine one or more fragments to build a single activity or even reuse fragments across multiple activities Mobile Application - Ishik
With (out) Fragments Without fragments, classically, developers would have to build a new Activity whenever the user interacted with the application. With Fragments, developers now can update another portion of the UI on the screen that corresponds to the user selection,, without needing to move the user to another screen” Mobile Application - Ishik
Fragments Fragments provide for developers ability to create a dynamic and multi-pane user interface on Android. You need to encapsulate UI components and activity behaviors into modules that you can swap into and out of your activities Fragment behaves somewhat like a nested activity that can define its own layout and manage its own lifecycle. Mobile Application - Ishik
Fragment’s Lifecycle A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle. For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments However, while an activity is running you can manipulate each fragment independently, such as add or remove them. Mobile Application - Ishik
Fragment and Back button When fragment performs a transaction, a back stack can be added. It can managed by the activity Each back stack entry in the activity is a record of the fragment transaction that occurred. The back stack allows the user to reverse a fragment transaction (navigate backwards), by pressing the Back button. Mobile Application - Ishik
Fragment and Back button When fragment performs a transaction, a back stack can be added. It can managed by the activity Each back stack entry in the activity is a record of the fragment transaction that occurred. The back stack allows the user to reverse a fragment transaction (navigate backwards), by pressing the Back button. Mobile Application - Ishik
Fragment Philosophy Mobile Application - Ishik
Fragment’s Lifecycle Mobile Application - Ishik
Creating Fragments Fragments can be crated using subclasses. It has the following callback methods: onCreate() onCreatView() onPause() Mobile Application - Ishik
Fragment – onCreat() The system calls this method when creating the fragment. It needs to initialize essential components of the fragment, when wants to retain on the following states: On paused On stopped, or On resumed. Mobile Application - Ishik
Fragment – onCratView() The system calls this method when the fragment needs to draw its users interface for the first time. It needs the View return methods to view the fragment. Mobile Application - Ishik
Fragment – onPause() The system calls this method as the first indication that the user is leaving the fragment. The onCommit () method will be involved. Mobile Application - Ishik
Crating a Fragment Crating a subclass from Fragments main class. This needed to tell that there is a Fragment in this activity. Mobile Application - Ishik
Crating a Fragment The parameters of crating the Fragments are automatically working in your code. Mobile Application - Ishik
Crating a Fragment The return inflate method. Mobile Application - Ishik
Mobile Application - Ishik
Adding Fragments Fragments need to be added inside the Activity. Mobile Application - Ishik
Class End Mobile Application - Ishik