Mobile Computing Fragments

Similar documents
UI Fragment.

Tablets have larger displays than phones do They can support multiple UI panes / user behaviors at the same time

Fragments were added to the Android API in Honeycomb, API 11. The primary classes related to fragments are: android.app.fragment

Fragments. Fragments may only be used as part of an ac5vity and cannot be instan5ated as standalone applica5on elements.

Multiple devices. Use wrap_content and match_parent Use RelativeLayout/ConstraintLayout Use configuration qualifiers

Activities and Fragments

Programming with Android: Android for Tablets. Dipartimento di Scienze dell Informazione Università di Bologna

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

Fragments. Lecture 11

Mobile Development Lecture 10: Fragments

ListView Containers. Resources. Creating a ListView

Understand applications and their components. activity service broadcast receiver content provider intent AndroidManifest.xml

States of Activities. Active Pause Stop Inactive

Fragments. Lecture 10

User Interface Development. CSE 5236: Mobile Application Development Instructor: Adam C. Champion Course Coordinator: Dr.

CS371m - Mobile Computing. More UI Navigation, Fragments, and App / Action Bars

Android Basics. Android UI Architecture. Android UI 1

CS371m - Mobile Computing. More UI Action Bar, Navigation, and Fragments

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

WebView Fragments Navigation Drawer

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

Programmatically Working with Android Fragments

Fragments and the Maps API

CSE 660 Lab 3 Khoi Pham Thanh Ho April 19 th, 2015

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Managing Screen Orientation

Action Bar. (c) 2010 Haim Michael. All Rights Reserv ed.

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Lifecycle Callbacks and Intents

Android Application Development

Lifecycle-Aware Components Live Data ViewModel Room Library

Android Application Model I

Building User Interface for Android Mobile Applications II

Creating a Custom ListView

EMBEDDED SYSTEMS PROGRAMMING UI and Android

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Overview of Activities

Vienos veiklos būsena. Theory

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

Repairing crashes in Android Apps. Shin Hwei Tan Zhen Dong Xiang Gao Abhik Roychoudhury National University of Singapore

Fragments may only be used as part of an activity and cannot be instantiated as standalone application elements.

Minds-on: Android. Session 2

Getting Started. Dr. Miguel A. Labrador Department of Computer Science & Engineering

Mobile Programming Lecture 7. Dialogs, Menus, and SharedPreferences

CMSC436: Fall 2013 Week 3 Lab

MVC Apps Basic Widget Lifecycle Logging Debugging Dialogs

Fragment Example Create the following files and test the application on emulator or device.

ANDROID USER INTERFACE

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

Comp 595MC/L: Mobile Computing CSUN Computer Science Department Fall 2013 Midterm

Adaptation of materials: dr Tomasz Xięski. Based on presentations made available by Victor Matos, Cleveland State University.

Topics of Discussion

Computer Science E-76 Building Mobile Applications

Chapter 5 Flashing Neon FrameLayout

FRAGMENTS.

Android Application Model I. CSE 5236: Mobile Application Development Instructor: Adam C. Champion, Ph.D. Course Coordinator: Dr.

Android CardView Tutorial

MODULE 2: GETTING STARTED WITH ANDROID PROGRAMMING

Android App Development. Mr. Michaud ICE Programs Georgia Institute of Technology

CS 4518 Mobile and Ubiquitous Computing Lecture 5: Rotating Device, Saving Data, Intents and Fragments Emmanuel Agu

CS378 - Mobile Computing. Anatomy of an Android App and the App Lifecycle

ActionBar. import android.support.v7.app.actionbaractivity; public class MyAppBarActivity extends ActionBarActivity { }

UNDERSTANDING ACTIVITIES

Android Activities. Akhilesh Tyagi

CS 4330/5390: Mobile Application Development Exam 1

Agenda. Overview of Xamarin and Xamarin.Android Xamarin.Android fundamentals Creating a detail screen

EMBEDDED SYSTEMS PROGRAMMING Application Basics

Lecture 14. Android Application Development

CS 528 Mobile and Ubiquitous Computing Lecture 3b: Android Activity Lifecycle and Intents Emmanuel Agu

Have a development environment in 256 or 255 Be familiar with the application lifecycle

Mobile User Interfaces

CS 4518 Mobile and Ubiquitous Computing Lecture 4: Data-Driven Views, Android Components & Android Activity Lifecycle Emmanuel Agu

Mobile Computing Practice # 2c Android Applications - Interface

INTRODUCTION TO ANDROID

CS 528 Mobile and Ubiquitous Computing Lecture 4a: Fragments, Camera Emmanuel Agu

Upon completion of the second part of the lab the students will have:

Our First Android Application

Android Programming: More User Interface. CS 3: Computer Programming in Java

Solving an Android Threading Problem

Mobile Application Development Android

Activities. Repo:

Theme 2 Program Design. MVC and MVP

Understanding Application

ARCHETYPE MODERN ANDROID ARCHITECTURE STEPAN GONCHAROV / DENIS NEKLIUDOV

Mobila applikationer och trådlösa nät, HI1033, HT2012

External Services. CSE 5236: Mobile Application Development Course Coordinator: Dr. Rajiv Ramnath Instructor: Adam C. Champion

More Effective Layouts

Lab 1: Getting Started With Android Programming

EMBEDDED SYSTEMS PROGRAMMING UI Specification: Approaches

Programming with Android: Introduction. Layouts. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Overview. What are layouts Creating and using layouts Common layouts and examples Layout parameters Types of views Event listeners

Android Exam AND-401 Android Application Development Version: 7.0 [ Total Questions: 129 ]

Hello World. Lesson 1. Android Developer Fundamentals. Android Developer Fundamentals. Layouts, and. NonCommercial

Mobila applikationer och trådlösa nät, HI1033, HT2013

CS 528 Mobile and Ubiquitous Computing Lecture 3: Android UI, WebView, Android Activity Lifecycle Emmanuel Agu

CHAPTER 4. Fragments ActionBar Menus

05. RecyclerView and Styles

Change in Orientation. Marco Ronchetti Università degli Studi di Trento

Instant Android Fragmentation Management How-to

Spring Lecture 5 Lecturer: Omid Jafarinezhad

Transcription:

Fragments APM@FEUP 1

Fragments (1) Activities are used to define a full screen interface and its functionality That s right for small screen devices (smartphones) In bigger devices we can have more interface elements and functionality e.g. a list of e-mails and the content of the selected item That s difficult to adapt using only activities Recent versions of Android defined the fragment interface APM@FEUP 2

Fragments (2) Fragments can be viewed as sub-activities They have their own View hierarchy (layout) They have their own lifecycle related to the activity lifecycle They can respond to the back button, like activities But fragments are contained inside an activity They use the same thread (the UI thread) They use the same context of the activity Fragments can coexist with other elements of the activity The activity layout can contain one or more fragments The fragment lifecycle is related with its activity lifecycle Lifecycle callbacks are called intermixed with the activity callbacks and executed by the same thread APM@FEUP 3

Fragment layouts They have their own internal layout They are included in activity layouts Permanently through the <fragment> tag Dynamically attaching and removing to some ViewGroup in the activity layout Usually a FrameLayout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:baselinealigned="false" android:orientation="horizontal" > <fragment android:id="@+id/titles" android:name="org.feup.apm.shakespearefragments.titlesfragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <FrameLayout android:id="@+id/details" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" /> </LinearLayout> class implementing the fragment placeholder for attaching fragments List Details APM@FEUP 4

Fragment lifecycle The fragment lifecycle follows the parent activity APM@FEUP 5

Entry sequence callbacks: Lifecycle callbacks (1) oninflate(activity activity, AttributeSet attrs, Bundle savedinstancestate) called in the beginning whenever the activity sets its content layout and has a <fragment> in it. The AttributeSet contains the attributes defined in the activity layout. They should be parsed and saved. The fragment is not yet attached to the activity. onattach(activity activity) The activity containing the fragment is now attached. You can save it, or get it, while the fragment is attached, using getactivity(). You can get the initialization arguments (set by setarguments() until this point) anytime after with getarguments(). oncreate(bundle savedinstancestate) ) called at the beginning of the owner activity oncreate() callback. Usually the activity View hierarchy is not yet inflated. You can create here another thread to do lengthy data loading operations. oncreateview(layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) Here you should return the inflated View of this fragment. If the container is null you should return null (the fragment will not be displayed). The container is the ViewGroup in the activity layout that will display the fragment. Do not attach the fragment to this container. Some specialized fragments (e.g. ListFragment) do not need to do this. onactivitycreated(bundle savedinstancestate) here the oncreate() method of the activity is now complete. The all interface is now built, including other fragments present. APM@FEUP 6

Lifecycle callbacks (2) onstart() and onresume() tied with the activity corresponding callbacks. The exit sequence of callbacks include the following, and they are called when the activity is also exiting or the fragment is being replaced (back button, other actions...) onpause() the first to be called (the fragment can be put on the fragment back stack). You should stop playing sounds, related to this fragment, here. onstop() tied with the onstop() callback of the activity. A stopped fragment can go strait to the onstart() callback. ondestroyview() when the fragment is being killed or saved this will be called. Here its View hierarchy is already detached from the activity layout. ondestroy() is called when the fragment is no longer in use (but still existing in the activity). ondetach() here the fragment does not belong anymore to the activity and the interface resources are already freed. onsaveintancestate(bundle outstate) called somewhere before ondestroy(). It should save internal state in the provided Bundle. That Bundle is passed to the entry callbacks. APM@FEUP 7

Fragment creation Fragments can be constructed by the system Whenever the activity inflates its layout and has a <fragment> element in it (static) They can be built in your code If you want to replace or attach a new fragment to some container (dynamic) In this case you should write a factory method in your fragment class (its recommended and even enforced) public static MyFragment newinstance(int index) { MyFragment f = new MyFragment(); Bundle args = new Bundle(); args.putint( index, index); f.setarguments(args); return f; The standard way to build a Fragment and pass it initialization parameters. They should be bundled and put in the class with setarguments(). They will be available inside the fragment code with getarguments(). APM@FEUP 8

FragmentManager Activities and Fragments can manipulate the active fragments That is the task of the FragmentManager object It is obtained with getfragmentmanager() from an Activity or Fragment It can find Fragments It can manipulate the fragment back stack It can save and restore references to fragments and fragment internal state Adding, replacing, removing, hiding and showing existing fragments must be done inside a transaction begintransaction() begins a new transaction and returns a FragmentTransaction object FragmentTransaction can do several operations on fragments At the end you should call commit() APM@FEUP 9

Example Suppose we have an activity showing some information, in a fragment, according to a list selection (position). The fragment, when constructed, is supplied with this position info, and its layout will contain the corresponding data. The handler for a new selection could be the following, replacing the fragment by a new one: // See if there is a fragment displayed already DetailsFragment details = (DetailsFragment) getfragmentmanager().findfragmentbyid(r.id.details); if (details == null details.getindexinfo()!= position) { // Make a new fragment to show this selection. details = DetailsFragment.newInstance(position); // Execute a transaction, replacing any existing fragment with the new one. FragmentTransaction ft = getfragmentmanager().begintransaction(); ft.setcustomanimations(r.animator.slide_in_left, R.animator.slide_out_right); ft.replace(r.id.details, details); // The transaction is added to the back stack to allow undoing it (with the back button) ft.addtobackstack( Details ); ft.commit(); APM@FEUP 10

Fragment implementation example <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scroller" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView> The Details fragment internal layout public class DetailsFragment extends Fragment { private int mindex = 0; public static DetailsFragment newinstance(int index) { DetailsFragment df = new DetailsFragment(); Bundle args = new Bundle(); args.putint("index", index); df.setarguments(args); return df; @Override public void oncreate(bundle mybundle) { super.oncreate(mybundle); mindex = getarguments().getint("index", 0); public int getshownindex() { return mindex; The Details fragment implementation @Override public View oncreateview(layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { if(container == null) return null; // Don't tie this fragment to anything through the inflater. View v = inflater.inflate(r.layout.details, container, false); TextView text1 = (TextView) v.findviewbyid(r.id.text1); text1.settext(shakespeare.dialogue[mindex] ); return v; APM@FEUP 11

DialogFragment The method of building dialogs from the Dialog class doesn t take care automatically of lifecycle events Embedding the dialog in a DialogFragment does that It also allows to display the whole dialog in a part of the screen, like a fragment Activity DialogFragment Dialog To show a dialog through the DialogFragment we need the FragmentManager call factory show() MyDialogFragment static factory MyDialog DialogFragment newfragment; newfragment = MyDialogFragment.newInstance( ); newfragment.show(getfragmentmanager(), tag"); oncreatedialog() build a Dialog return Dialog APM@FEUP 12