ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

Similar documents
ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

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

Minds-on: Android. Session 2

Android Activities. Akhilesh Tyagi

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

States of Activities. Active Pause Stop Inactive

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

Overview of Activities

UI Fragment.

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

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

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

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

Understanding Application

UNDERSTANDING ACTIVITIES

Activities. Repo:

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

LECTURE NOTES OF APPLICATION ACTIVITIES

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

Mobile Computing Fragments

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

Fragments and the Maps API

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

Android Basics. Android UI Architecture. Android UI 1

Activities and Fragments

CMSC436: Fall 2013 Week 3 Lab

Android Fundamentals - Part 1

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

Lab 1: Getting Started With Android Programming

Android Application Development

Application Fundamentals

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

Lecture 2 Android SDK

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

EMBEDDED SYSTEMS PROGRAMMING Application Basics

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

Introduction to Android

Building User Interface for Android Mobile Applications II

COLLEGE OF ENGINEERING, NASHIK-4

Lifecycle-Aware Components Live Data ViewModel Room Library

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

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

CS 4330/5390: Mobile Application Development Exam 1

Introduction to Android development

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

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

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

Lifecycle Callbacks and Intents

CE881: Mobile & Social Application Programming

Android. Mobile operating system developed by Google A complete stack. Based on the Linux kernel Open source under the Apache 2 license

application components

Real-Time Embedded Systems

Embedded Systems Programming - PA8001

Programming Android UI. J. Serrat Software Design December 2017

Programming in Android. Nick Bopp

Mobile Application Development Android

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

MODULE 2: GETTING STARTED WITH ANDROID PROGRAMMING

Android Ecosystem and. Revised v4presenter. What s New

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

COSC 3P97 Mobile Computing

Android Programmierung leichtgemacht. Lars Vogel

EMBEDDED SYSTEMS PROGRAMMING UI and Android

Android for Ubiquitous Computing Researchers. Andrew Rice University of Cambridge 17-Sep-2011

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

Vienos veiklos būsena. Theory

Mobile User Interfaces

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

Mobile Development Lecture 10: Fragments

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

INTRODUCTION TO ANDROID

CPET 565 Mobile Computing Systems CPET/ITC 499 Mobile Computing. Lab & Demo 2 (Part 1-2) Hello-Goodbye App Tutorial

ANDROID MOCK TEST ANDROID MOCK TEST IV

Services are software components designed specifically to perform long background operations.

Programming with Android: Intents. Luca Bedogni. Dipartimento di Scienze dell Informazione Università di Bologna

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

Android Basics. - Bhaumik Shukla Android Application STEALTH FLASH

Android System Architecture. Android Application Fundamentals. Applications in Android. Apps in the Android OS. Program Model 8/31/2015

CPET 565 Mobile Computing Systems CPET/ITC 499 Mobile Computing. Lab & Demo 2 (1 &2 of 3) Hello-Goodbye App Tutorial

CHAPTER 4. Fragments ActionBar Menus

CE881: Mobile & Social Application Programming

Introduction to Android

CS 193A. Activity state and preferences

Answers to Exercises

Mobile Computing. Introduction to Android

Android framework overview Activity lifecycle and states

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

You ve been told lies about Fragments.

GUI Design for Android Applications

Midterm Examination. CSCE 4623 (Fall 2017) October 20, 2017

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Mobile Development Lecture 11: Activity State and Preferences

Mobile Application Programing: Android. View Persistence

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

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

ANDROID DEVELOPMENT. Course Details

CS378 -Mobile Computing. Services and Broadcast Receivers

Google Maps Troubleshooting

Fragments. Lecture 11

Programming Mobile Applications with Android Lab2

Transcription:

ACTIVITY, FRAGMENT, NAVIGATION Roberto Beraldi

Introduction An application is composed of at least one Activity GUI It is a software component that stays behind a GUI (screen) Activity It runs inside the main thread and it should be reactive to user s input Managed by the Activity Manager

Activity and anctivity manager GUI user input events Activity register with Activity Manager callback methods

States of an activity Running: the activity is in foreground and has the focus All events are delivered to it Paused: it is partially visible, but lost the focus For example the foreground activity doesn t occupy the whole screen (there is a dialog box in foreground) A paused activity maintains all state and member information, but the system can kill it Stopped: the activity is completely obscured by another one

Process, Task, Thread Each application runs inside its own Process Usually, other threads are created to peform long running operations The set of activities lunched by a user is a Task History based navigation across activities is obtained through a LIFO back stack The activity in a task can belong to different applications For example picking a contact will lunch the contact application,

Back stack Activities in the system are managed via an back stack When a new activity is created, it is placed on the top of the stack and becomes active The previous activity remains below in the stack until the running activity is deleted (for example back button) Running activity Ak A2 A1

Back stack

Activity lifecycle Each activity in an application goes through its own lifecycle: it responds to a set of methods called by android Once and only once when an activity is created, is the oncreate( ) method executed If the activity exits, the ondestroy() method is executed In between, various methods are called allowing the activity to be harmonically managed

Lifecycle states In the Paused or Stopped state an activity can be killed by the am when resources are needed to the OS.

Lifecycle methods Method: oncreate(bundle savedinstancestate) Called when the activity is first created This is where normal static initialization should go, e.g., create views, bind data to list, etc. This method is passed a Bundle object containing the activity s previous state (null the first time). Always followed by onstart()

Lifecycle methods Method: onstart() Called just before the activity becomes visible to the user or when the activity is becoming visible again Put code that can affect UI content Register a Broadcast receiver (see later)

Lifecycle methods Method: onresume() Called just before the activity starts interacting with the user. At this point the activity is at the top of the activity stack, with user input going to it. This is a good place to begin animations

Running state

Lifecycle methods Method: onpause() Called when the system is about to start resuming another activity. This method is typically used to commit unsaved changes to persistent data, stop animations, threads, and any thing that may be consuming CPU, and so on. It should do whatever it does very quickly, because the next activity will not be resumed until it returns. Followed either by onresume() if the activity returns back to the front, or by onstop() if it becomes invisible to the user. After this method, the activity is killable by the system.

Lifecycle methods Method: onstop() Called when the activity is no longer visible to the user. This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it. Followed either by onrestart() if the activity is coming back to interact with the user, or by ondestroy() if this activity is going away.

Lifecycle methods Method: onrestart() Called after the activity has been stopped, just prior to it being started again. Method: ondestroy() Called before the activity is destroyed. This is the final call that the activity will receive. It could be called either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space. One can distinguish between these two scenarios with the isfinishing() method.

Example: change the device orientation oncreate onstart onresume onpause onsaveinstancestate onstop ondestroy oncreate onstart onrestoreinstancestate onresume

How an activity is created (explicitally)? An activity is created if it is the target of a special message called Intent An activity A starts another activity B by creating an explicit Intent with target B Uses a special method Context.startActivity(intent)

How an activity is created (implicitally)? An activity declares to the system its ability to perform actions through an intent-filter (declared in the manifest) The calling activity creates and intent with the action The activity manager presents a list of all activities that may perform the action (if more than one) Activity A start( ) Activity Action? Activity Manager

Launching an Activity Inside an application, an activity is marked as able to respond to action MAIN The launcher populates the home screen by retriving the activity can can respond the the MAIN action, category LAUNCHER When the icon is pushed, the Activity tagged as MAIN is started Indeed a new process is created from Zygote with shared libraries preloaded and specialized for the application (see:https://anatomyofandroid.com/)

ActivityLifecycle (demo) android:theme="@android:style/theme.dialog" I/INFO : A: oncreate I/INFO : A: onstart I/INFO : A: onresume (Button is clicked) I/INFO : A: onpause I/INFO : B: oncreate I/INFO : B: onstart I/INFO : B: onresume I/INFO : A: onsaveinstancestate (now the activity can be killed ) (back botton is pressed) I/INFO : B: onpause I/INFO : A: onresume I/INFO : B: onstop I/INFO : B: ondestroy note method calls are interleaved

ActivityLifeCycle (demo) I/INFO : A: oncreate I/INFO : A: onstart I/INFO : A: onresume (second acivity is lunched an takes all the screen) I/INFO : A: onpause I/INFO : B: oncreate I/INFO : B: onstart I/INFO : B: onresume I/INFO : A: onsaveinstancestate I/INFO : A: onstop (First activity is no longer visible) I/INFO : B: onpause (back button is now pressed) I/INFO : A: onrestart I/INFO : A: onstart I/INFO : A: onresume I/INFO : B: onstop I/INFO : B: ondestroy

Should you implement all methods? All of these methods are hooks that one can override to do appropriate work when the state changes. All activities must implement oncreate() to do the initial setup when the object is first instantiated. It will also important (recommended) to implement onpause() to commit data changes and otherwise prepare to stop interacting with the user.

Saving the state Before to be killable, the onsaveinstancestate(bundle savedinstancestate) is called Here one can store specific activity s state variables (in the bundle) The Bundle is passed to oncreate method and to onrestoreinstancestate method

Saving the state

ActivityManager (demo)

Fragments Useful for large screen (like tablet) or as navigation tools Fragments are hosted inside an Activity A fragment is attached to a View It must create its view (from an XML file) Fragments have their own lifecycle (which is more complex than the activity) Can be added via XML file or programmatically

Fragments and Activities Activity Fragment A CV Fragment CV B getactivity() Activity s lifecycle methods Fragment specific lifecycle methods Fragment Manager

Fragment lifecycle

Using Fragments

FragmentXML (demo) Two fragments One is ListFragment Activity is the glue among them Warning: onattach method changed in API 23!

Fragment via XML activity_main LinearLayout <fragment android:name=«.»> implementation Fragment Code Inflates firstfragement.xml (oncreateview) <fragment android:name=«.»> Fragment Code implementation Extends a ListFragment Activity Code implementation Inflates activity_main.xml

FragmentProgram (demo)

Fragments added programmatically Use a FragmentManager to add, remove fragments. After changes, use commit activity_main FrameLayout, id=1 placeholder (fragment container) FrameLayout, id=2

Screen navigation options Back navigation Hierarchical navigation Up navigation Descendant navigation Navigation drawer for descendant navigation Lists and carousels for descendant navigation Ancestral navigation Lateral navigation

Type of hierarchical navigation Descendant navigation Down from a parent screen to one of its children From a list of headlines to a story summary to a story Ancestral or up navigation Up from a child or sibling screen to its parent From a story summary back to the headlines Lateral navigation From one sibling to another sibling Swiping between tabbed views

Descendant navigation (with a list) News App Top Stories Tech News Cooking Story Story Story

Descendant navigation (fragments)

Descendant navigation (navigation drawer) 1.Icon in app bar 2.Header 3.Menu items

Up navigation

Lateral navigation News App Top Stories Tech News Cooking Story Story Story

A complete example

QUESTIONS?