Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android Fragments. Slide 2 Lecture Overview Getting resources and configuration information Conceptualizing the Back Stack Introduction to fragments.

Similar presentations


Presentation on theme: "Android Fragments. Slide 2 Lecture Overview Getting resources and configuration information Conceptualizing the Back Stack Introduction to fragments."— Presentation transcript:

1 Android Fragments

2 Slide 2 Lecture Overview Getting resources and configuration information Conceptualizing the Back Stack Introduction to fragments

3 Slide 3 Introduction to Resources Anything that is not code (java), is considered a resource Layouts, strings, and all of the XML are resources The Resources class is used to access this information The getResources() method of the application Context gets the current class instance

4 Slide 4 Resources (Types) The Resources class has many child types, which categorize the various resources Configuration class contains information about the device’ and user configuration DisplayMetrics class contains current information about the display There are many more

5 Slide 5 Configuration Information android.content.res.Configuration contains configuration information about your device Screen size (based on orientation): screenHeightDP, screenWidthDP Orientation contained in the orientation property: ORIENTATION_LANDSCAPE, ORIENTATION_PORTRAIT

6 Slide 6 Configuration Information The following statement gets the configuration

7 Slide 7 DisplayMetrics Class Gets physical device characteristics widthPixels heightPixels densityDpi and many more DisplayMetrics metrics = getResources().getDisplayMetrics(); int widthPixels = metrics.widthPixels; int heightPixels = metrics.heightPixels; int densityDpi = metrics.densityDpi;

8 Slide 8 Working with Different Orientations There is not much to do Crate a folder named layout-land in the res folder Create the activity layout file. Both the portrait and landscape layouts share the same Java file Android knows the orientation and will render the correct form Does not work quite right in the emulator

9 Slide 9 The Back Stack (Introduction) An application is made up of multiple activities And as you know one activity can start another activity, which can, in turn start another activity Activities are connected together as a task Whether in the same or different applications This sequence of activities in a task is called the back stack

10 Slide 10 The Back Stack (Illustration)

11 Slide 11 Fragments (Introduction) A fragment is a behavior or part of a user interface that can be placed withinin an activity Use it to create a multi-pane UI Reuse the same fragment in multiple activities Think of a fragment as a modular section of an activity which: Has its own lifecycle Receives its own input events

12 Slide 12 Fragments (Characteristics) Fragments are always embedded in an activity Fragments are controller objects An activity’s view contains visual space where the fragment’s view will be inserted An activity can have several fragments Fragments were introduced at API level 11 along with the first tables There is a support library for older versions

13 Slide 13 Fragments (Illustration 1) Activity with two fragments (tablet)

14 Slide 14 Fragments (Illustration 2) Activity with two fragments (handset)

15 Slide 15 The Fragment Lifecycle It operates similar to the activity lifecycle When an activity is paused, all fragments in it are paused Fragments can be manipulated independently of the parent activity

16 Slide 16 The Fragment Lifecycle

17 Slide 17 Overriding Fragment Methods (Create) To create a fragment you create a subclass of Fragment (looks like an activity) You almost always implement the following methods The system calls onAttach(), when the fragment is associated with an activity The system calls onCreate() when creating the fragment

18 Slide 18 Overriding Fragment Methods (Create) The systems calls onCreateView() when the fragment is rendered the first time onActivtyCreated() tells the fragment that the activity has completed it’s call to Activity.OnCreate() onStart() makes the fragment visible (same as activity) onResume() starts user interaction (same as activity)

19 Slide 19 Overriding Fragment Methods (Destroy) The system calls onPause() when the user is leaving the fragment onStop() is called when the fragment is no longer visible onDestroyView() allows the fragment to clean up resources associated with the currentview onDestroy() is called to do final cleanup onDetach() is called when the fragment is disassociated with the activity

20 Slide 20 Hosting Fragments There are two ways to host fragments: In the activity’s layout (static definition) Inflexible because the fragment cannot be swapped out during the activity lifetime In the activity’s code (dynamic definition) Better because you can dynamically add and remove different fragments More complicated

21 Slide 21 Hosting Fragments (Static Definition)

22 Slide 22 Hosting Fragments (Layout) You connect the layout using the same techniques that you have seen

23 Slide 23 Hosting Fragments (Code) Add a fragment to an existing ViewGroup (Layout) The FragmentManager is responsible for managing fragments and adding views to the activity’s view hierarchy It manages A list of fragments And a back stack of fragment transactions

24 Slide 24 FragmentManager (Illustration)

25 Slide 25 Getting the Fragment Manager Use getFragmentManager() to get the current fragment manager If you need compatibility with older versions, use getSupportFragmentManager() FragmentManager fragmentManager = getFragmentManager()getFragmentManager()

26 Slide 26 Managing Fragments (Introduction) The FragmentManager allows you to Get the fragments associated with an activity Create a fragment transaction, which, in turn, affects the back stack (add, replace, remove) beginTransaction() method starts a series of edit operations on the fragments associated with this FragmentManager The method returns a FragmentTransaction

27 Slide 27 Managing Fragments Using the FragmentManager you can Get activity fragments with findFragmentById() or findFragmentByTag() Pop fragments off the back stack with popBackStack() Acts like the user clicking the back button Register a listener for back stack changes with addOnBackStackChangedListener()

28 Slide 28 The FragmentTransaction call beginTransaction() to start a fragment transaction and commit () to cause the changes to take effect In between the two calls add places one fragment on top of another fragment (add to the back stack) replace causes the current fragment to be destroyed specified fragment created (replace item in the back stack

29 Slide 29 The FragmentTransaction (Example) Create a fragment and transaction,

30 Slide 30 Fragment (Best Practices) The following methods are commonly implemented Display a floating dialog with DialogFragment Display a list with ListFragment Create a setting activity with PreferenceFragment

31 Slide 31 // 32


Download ppt "Android Fragments. Slide 2 Lecture Overview Getting resources and configuration information Conceptualizing the Back Stack Introduction to fragments."

Similar presentations


Ads by Google