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

Slides:



Advertisements
Similar presentations
Programming with Android: Android for Tablets Luca Bedogni Marco Di Felice Dipartimento di Scienze dellInformazione Università di Bologna.
Advertisements

Programming with Android: Activities
Programming with Android: Android for Tablets Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
CE881: Mobile and Social Application Programming Simon M. Lucas Menus and Dialogs.
Fragments: Introduction Fragments were introduced in Android 3.0 to support flexible and dynamic UI designs represent portions of an application’s user.
Programming with Android: Activities
Android 02: Activities David Meredith
All About Android Introduction to Android 1. Creating a New App “These aren’t the droids we’re looking for.” Obi-wan Kenobi 1. Bring up Eclipse. 2. Click.
The Android Activity Lifecycle. Slide 2 Introduction Working with the Android logging system Rotation and multiple layouts Understanding the Android activity.
The Activity Class 1.  One application component type  Provides a visual interface for a single screen  Typically supports one thing a user can do,
Filip Debelić What is it? Android is a mobile operating system (OS) based on the Linux kernel and currently developed by Google Android,
Android Fragments.
More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View.
By: Jeremy Smith.  Introduction  Droid Draw  Add XML file  Layouts  LinearLayout  RelativeLayout  Objects  Notifications  Toast  Status Bar.
Programming with Android: Android Fragments Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Android UI, and Networking. Can do most networking on Android Bluetooth only on 2.0, Not supported with version 1.6.
Android Fragments A very brief introduction Android Fragments1.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
Android: versions Note that: Honeycomb (Android v3.0) A tablet-only release Jelly Bean (Android v4.1) Released on July 09, 2012.
ANDROID UI – FRAGMENTS. Fragment  An activity is a container for views  When you have a larger screen device than a phone –like a tablet it can look.
Android Mobile computing for the rest of us.* *Prepare to be sued by Apple.
Chapter 2: Simplify! The Android User Interface
Tip Calculator App Building an Android App with Java © by Pearson Education, Inc. All Rights Reserved.
Mobile Programming Lecture 6
Activities and Intents. Activities Activity is a window that contains the user interface of your application,typically an application has one or more.
INTRODUCTION TO ANDROID. Slide 2 Application Components An Android application is made of up one or more of the following components Activities We will.
SpotOn Game App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Programming Mobile Applications with Android September, Albacete, Spain Jesus Martínez-Gómez.
Understand applications and their components activity service broadcast receiver content provider intent AndroidManifest.xml.
Android Boot Camp for Developers Using Java, 3E
Android – Fragments L. Grewe.
Cosc 4730 Android Fragments. Fragments You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own.
Noname. Conceptual Parts States of Activities Active Pause Stop Inactive.
ANDROID AND MODEL / VIEW / CONTROLLER. Slide 2 Design Patters Common solutions to programming problems are called design patterns Design patterns are.
Activities Димитър Н. Димитров Astea Solutions AD.
More UI Action Bar, Navigation, and Fragments
CS378 - Mobile Computing More UI. UI Review Containers – more formally ViewGroups – store widgets and other containers – examples: LinearLayout, RelativeLayout,
Class on Fragments & threads. Fragments fragment is a modular section of an activity, which has its own lifecycle, receives its own input events, and.
CHAPTER 4 Fragments ActionBar Menus. Explore how to build applications that use an ActionBar and Fragments Understand the Fragment lifecycle Learn to.
ANDROID LAYOUTS AND WIDGETS. Slide 2 Introduction Parts of the Android screen Sizing widgets and fonts Layouts and their characteristics Buttons, checkboxes.
School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,
The Flag Quiz app tests your ability to correctly identify 10 flags from various countries and territories.
Fragments and Menus Chapter 4 1. Objectives Learn three different types of menus: options, context, and popup Learn to configure the ActionBar and Toolbar.
Open Handset Alliance.
Activity and Fragment.
Activities, Fragments, and Events
Fragment ?.
Fragments: Introduction
Android 3: Fragments: API Guide
CS499 – Mobile Application Development
Mobile Application Development BSCS-7 Lecture # 6
Android 16: Fragments: Tutorial
Mobile Applications (Android Programming)
Activities and Intents
Android Activities An application can have one or more activities, where Each activity Represents a screen that an app present to its user Extends the.
Widgets & Fragments Kalin Kadiev Astea Solutions AD.
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
Android 15: Fragments: API Guide
The Android Activity Lifecycle
Android – Fragments L. Grewe.
ANDROID UI – FRAGMENTS UNIT II.
CIS 470 Mobile App Development
Chapter 9: Fragments.
Activities and Intents
HNDIT2417 Mobile Application Development
CIS 470 Mobile App Development
Mobile Programming Dr. Mohsin Ali Memon.
Activities and Fragments
Activities, Fragments, and Intents
Android Sensor Programming
Presentation transcript:

Android Fragments

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

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

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

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

Slide 6 Configuration Information The following statement gets the configuration

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;

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

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

Slide 10 The Back Stack (Illustration)

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

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

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

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

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

Slide 16 The Fragment Lifecycle

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

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)

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

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

Slide 21 Hosting Fragments (Static Definition)

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

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

Slide 24 FragmentManager (Illustration)

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()

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

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()

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

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

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

Slide 31 // 32