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

Similar documents
UI Fragment.

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

Mobile Computing Fragments

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

Fragments. Lecture 11

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

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

Programmatically Working with Android Fragments

Activities and Fragments

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

Building User Interface for Android Mobile Applications II

Mobile Development Lecture 10: Fragments

States of Activities. Active Pause Stop Inactive

CS 4330/5390: Mobile Application Development Exam 1

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

Overview of Activities

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

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

CHAPTER 4. Fragments ActionBar Menus

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Managing Screen Orientation

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

Lifecycle-Aware Components Live Data ViewModel Room Library

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

CMSC436: Fall 2013 Week 3 Lab

Fragments. Lecture 10

Fragments and the Maps API

Android Application Development

Activities. Repo:

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

UNDERSTANDING ACTIVITIES

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

ANDROID USER INTERFACE

Android Basics. Android UI Architecture. Android UI 1

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

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

Topics of Discussion

FRAGMENTS.

MODULE 2: GETTING STARTED WITH ANDROID PROGRAMMING

EMBEDDED SYSTEMS PROGRAMMING Application Basics

EMBEDDED SYSTEMS PROGRAMMING UI and Android

LECTURE NOTES OF APPLICATION ACTIVITIES

Vienos veiklos būsena. Theory

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

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

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

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

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

WebView Fragments Navigation Drawer

Computer Science E-76 Building Mobile Applications

Minds-on: Android. Session 2

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

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

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

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

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

EMBEDDED SYSTEMS PROGRAMMING UI Specification: Approaches

Solving an Android Threading Problem

You ve been told lies about Fragments.

Lab 1: Getting Started With Android Programming

Lifecycle Callbacks and Intents

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

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

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

Android Activities. Akhilesh Tyagi

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

Instant Android Fragmentation Management How-to

Android Fundamentals - Part 1

Mobile Computing Practice # 2c Android Applications - Interface

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

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

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

Mobile User Interfaces

Xin Pan. CSCI Fall

MVC Apps Basic Widget Lifecycle Logging Debugging Dialogs

Android Application Model I

Mobile Application Development MyRent Settings

Intents. Your first app assignment

More Effective Layouts

ANDROID MOCK TEST ANDROID MOCK TEST IV

Android development. Outline. Android Studio. Setting up Android Studio. 1. Set up Android Studio. Tiberiu Vilcu. 2.

Android CardView Tutorial

android:orientation="horizontal" android:layout_margintop="30dp"> <Button android:text="button2"

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

Topics of Discussion

Understanding Application

ELET4133: Embedded Systems. Topic 15 Sensors

CS 370 Android Basics D R. M I C H A E L J. R E A L E F A L L

Screen Slides. The Android Studio wizard adds a TextView to the fragment1.xml layout file and the necessary code to Fragment1.java.

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

Our First Android Application

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

COLLEGE OF ENGINEERING, NASHIK-4

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

Introduction to Android

University of Babylon - College of IT SW Dep. - Android Assist. Lect. Wadhah R. Baiee Activities

Arrays of Buttons. Inside Android

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

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

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Switching UIs

Let s take a display of HTC Desire smartphone as an example. Screen size = 3.7 inches, resolution = 800x480 pixels.

Transcription:

Tablets have larger displays than phones do They can support multiple UI panes / user behaviors at the same time The 1 activity 1 thing the user can do heuristic may not make sense for larger devices

Application uses two Activities Ones shows titles of Shakespeare plays & allows user to select one title The other shows a quote from the selected play

FragmentQuoteViewer WithActivity

This layout is reasonable on a phone But inefficient on a larger device

Use two cooperating layout units on one screen

Typically represents a behavior / portion of UI Multiple Fragments can be embedded in an Activity to create a multi-pane UI A single Fragment can be reused across multiple Activities

Fragment lifecycle is coordinated with the lifecycle of its containing/hosting Activity Fragments have their own lifecycles and receive their own callbacks

Resumed Fragment is visible in the hosting Activity Paused Another Activity is in the foreground and has focus, this Fragment s hosting Activity is still visible Stopped The Fragment is not visible

onattach() Activity is created Fragment is first attached to its Activity

onattach() Initialize the Fragment oncreate()

onattach() Fragment sets up & returns its user interface View oncreate() oncreateview()

onattach() Containing Activity has completed oncreate() and the Fragment has been installed Can now access hosting Activity oncreate() oncreateview() onactivitycreated()

onstart () Activity is started Hosting Activity about to become visible

onresume() Activity is resumed Hosting Activity is about to become visible and ready for user interaction

onpause() Activity is paused Hosting Activity is visible, but does not have focus

onstop () Activity is stopped Hosting Activity is no longer visible

ondestroyview() Activity is destroyed View previously created in oncreateview() has been detached from the Activity Clean up view resources

ondestroyview() Fragment is no longer in use Clean up Fragment resources ondestroy()

ondestroyview() Fragment no longer attached to its activity Null out references to hosting Activity ondestroy() ondetach()

Two general ways to add Fragments to an Activity s layout Declare it statically in the Activity s layout file Add it programmatically using the fragmentmanager

Layout can be inflated/implemented in oncreateview() oncreateview() must return the View at the root of the Fragment s layout This View is added to the containing Activity

Display titles and quotes side-by-side in two Fragments Fragments are statically added based on a layout file

FragmentStaticLayout

public class QuoteViewerActivity extends Activity implements ListSelectionListener { @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.quote_activity); }

<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" android:padding="@dimen/activity_margin"> <fragment android:id="@+id/titles" class="course.examples.fragments.staticlayout.titlesfragment" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/details" class="course.examples.fragments.staticlayout.quotesfragment" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="2" /> </LinearLayout>

Fragments should be reusable across Activities Avoid coupling Fragments i.e, Frag1 should not directly interact with Frag2 Coupling should be handled by callbacks to hosting Activity

// Callback interface that defines how a TitlesFragment notifies the QuoteViewerActivity // when user clicks on a List Item in the TitlesFragment interface ListSelectionListener { void onlistselection(int index); }

public class TitlesFragment extends ListFragment { // Called when the user selects an item from the List public void onlistitemclick(listview l, View v, int pos, long id) { // Inform the QuoteViewerActivity that the item in position pos has been selected mlistener.onlistselection(pos); } public void onattach(activity activity) { // Set the ListSelectionListener for communicating with the QuoteViewerActivity mlistener = (ListSelectionListener) activity; }

public class QuoteViewerActivity extends Activity implements ListSelectionListener { // Called by TitlesFragment when the user selects an item public void onlistselection(intindex) { // Tell the QuoteFragment to show the quote string at position index mquotesfragment.showquoteatindex(index); } }

While an Activity is running you can add and remove Fragments in its layout Four-step process 1. Get reference to the FragmentManager 2. Begin a FragmentTransaction 3. Add the Fragment 4. Commit the FragmentTransaction

Display titles and quotes side-by-side in two Fragments Layout file reserves space for Fragments Fragments are programmatically added to layout at runtime

Fragment ProgrammaticLayout

protected void oncreate(bundle savedinstancestate) { // Get a reference to the FragmentManager FragmentManager fragmentmanager = getfragmentmanager(); if (null == fragmentmanager.findfragmentbyid(r.id.title_frame)) { // Begin a new FragmentTransaction FragmentTransaction fragmenttransaction = fragmentmanager.begintransaction(); // Add the TitleFragment fragmenttransaction.add(r.id.title_frame, new TitlesFragment()); // Add the QuoteFragment mquotefragment = new QuotesFragment(); fragmenttransaction.add(r.id.quote_frame, mquotefragment); // Commit the FragmentTransaction fragmenttransaction.commit(); } else { mquotefragment = (QuotesFragment) fragmentmanager.findfragmentbyid(r.id.quote_frame); } }

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activityframe" android:layout_width="match_parent" android:layout_height="match_parent" android:baselinealigned="false" android:orientation="horizontal" android:padding="@dimen/activity_margin"> <FrameLayout android:id="@+id/title_frame" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <FrameLayout android:id="@+id/quote_frame" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" /> </LinearLayout>

Fragment transactions allow you to dynamically change your app s user interface Can make the interface more fluid & take better advantage of available screen space

Starts with a single Fragment Changes to two-fragment layout when user selects a title

FragmentDynamic Layout

FragmentDynamic Layout

protected void oncreate(bundle savedinstancestate) { // Get a reference to the FragmentManager mfragmentmanager = getfragmentmanager(); mquotefragment = (QuotesFragment) mfragmentmanager.findfragmentbyid(r.id.quote_fragment_container); mtitlefragment = ( TitlesFragment) mfragmentmanager.findfragmentbyid(r.id.title_fragment_container); if (null == mfragmentmanager.findfragmentbyid(r.id.title_fragment_container)) { // Start a new FragmentTransaction FragmentTransaction fragmenttransaction = mfragmentmanager.begintransaction(); mtitlefragment = new TitlesFragment(); // Add the TitleFragment to the layout fragmenttransaction.add(r.id.title_fragment_container, mtitlefragment); // Commit the FragmentTransaction fragmenttransaction.commit(); }

public void onlistselection(int index) { // If the QuoteFragment has not been created, create and add it now if (null == mfragmentmanager.findfragmentbyid(r.id.quote_fragment_container)) { mquotefragment = new QuotesFragment(); // Start a new FragmentTransaction FragmentTransaction fragmenttransaction = mfragmentmanager.begintransaction();

// Add the QuoteFragment to the layout fragmenttransaction.add(r.id.quote_fragment_container, mquotefragment); // Add this FragmentTransaction to the backstack fragmenttransaction.addtobackstack(null); // Commit the FragmentTransaction fragmenttransaction.commit(); // Force Android to execute the committed FragmentTransaction mfragmentmanager.executependingtransactions(); } // Tell the QuoteFragment to show the quote string at position index mquotefragment.showquoteatindex(index); }

If you call setretaininstance(true) on a Fragment, Android won t destroy that Fragment on configuration changes

Results in some changes to lifecycle callback sequence ondestroy() will not be called oncreate() will not be called

Essentially the same as FragmentStaticLayout Focus here is on how Fragments are saved and restored on configuration changes

In landscape mode Both Fragments use a large font TitleFragment takes more horizontal space & allows long titles to span multiple lines

In portrait mode Both Fragments use a smaller font TitleFragment will use less space and will ellipsize long titles, limiting them to a single line

FragmentStatic ConfigLayout

public class QuotesFragment extends Fragment { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // Retain this Fragment across Activity reconfigurations setretaininstance(true); }

// Set up some information about the mquoteview TextView public void onactivitycreated(bundle savedinstancestate) { mquoteview = getactivity().findviewbyid(r.id.quoteview); mquotearraylen = QuoteViewerActivity.mQuoteArray.length; } showquoteatindex(mcurridx);

User Interface classes

FragmentQuoteViewerWithActivity FragmentStaticLayout FragmentProgrammaticLayout FragmentDynamicLayout FragmentStaticConfigLayout