Minds-on: Android. Session 2

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

States of Activities. Active Pause Stop Inactive

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

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

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

Android Activities. Akhilesh Tyagi

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

Activities and Fragments

Application Fundamentals

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

Android Fundamentals - Part 1

Understanding Application

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

Overview of Activities

CMSC436: Fall 2013 Week 3 Lab

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

Lecture 2 Android SDK

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

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

Activities. Repo:

Android Basics. Android UI Architecture. Android UI 1

LECTURE NOTES OF APPLICATION ACTIVITIES

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

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

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

Introduction to Android

Lifecycle-Aware Components Live Data ViewModel Room Library

UI Fragment.

application components

UNDERSTANDING ACTIVITIES

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

Programming in Android. Nick Bopp

Android Ecosystem and. Revised v4presenter. What s New

Mobile Computing. Introduction to Android

Minds-on: Android. Session 1

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

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

Lab 1: Getting Started With Android Programming

CS 193A. Activity state and preferences

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

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

Introduction to Android development

Fragments and the Maps API

Lifecycle Callbacks and Intents

Mobile Development Lecture 11: Activity State and Preferences

Android Application Development

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

COSC 3P97 Mobile Computing

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

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

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

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

INTRODUCTION TO ANDROID

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

EMBEDDED SYSTEMS PROGRAMMING Application Basics

COLLEGE OF ENGINEERING, NASHIK-4

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

Static Detection of Energy Defect Patterns in Android Applications

CS378 -Mobile Computing. Services and Broadcast Receivers

Mobile Computing Fragments

CS378 -Mobile Computing. Intents

Android Programmierung leichtgemacht. Lars Vogel

2017 Pearson Educa2on, Inc., Hoboken, NJ. All rights reserved.

Camera and Intent. Elena Fortini

Android framework overview Activity lifecycle and states

CS 4330/5390: Mobile Application Development Exam 1

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

Android App Development

Vienos veiklos būsena. Theory

Developer s overview of the Android platform

Software Practice 3 Today s lecture Today s Task

Why using intents. Activity. Screen1 Screen 2

Mobile User Interfaces

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

Mobile Application Development Android

Building User Interface for Android Mobile Applications II

Understanding and Detecting Wake Lock Misuses for Android Applications

Mobile Application Programing: Android. View Persistence

Answers to Exercises

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

Services. service: A background task used by an app.

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

Understanding and Detecting Wake Lock Misuses for Android Applications

Syllabus- Java + Android. Java Fundamentals

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Introduction to Android

User Interface Design & Development

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

MC Android Programming

Embedded Systems Programming - PA8001

Programming Mobile Applications with Android Lab2

ANDROID SYLLABUS. Advanced Android

Programming with Android: Activities and Intents. Dipartimento di Informatica Scienza e Ingegneria Università di Bologna

Android App Development. Muhammad Sharjeel COMSATS Institute of Information Technology, Lahore

Intents and Intent Filters

CE881: Mobile & Social Application Programming

Real-Time Embedded Systems

Android. Operating System and Architecture. Android. Screens. Main features

EMDK for Android Workshops Prashanth Kadur

Android Basics. - Bhaumik Shukla Android Application STEALTH FLASH

Transcription:

Minds-on: Android Session 2 Paulo Baltarejo Sousa Instituto Superior de Engenharia do Porto 2016

Outline Activities UI Events Intents Practice Assignment 1 / 33

2 / 33 Activities

Activity An activity provides a user interface (UI) for a single screen in an app Each activity is given a window in which is drawn its UI. The window typically fills the screen, but may be smaller than the screen and float on top of other windows. To create an activity, you must create a subclass of Activity (or an existing subclass of it. An app usually consists of multiple activities. One activity in an app is specified as the main activity, which is presented to the user when launching the app for the first time (defined in the manifest.xml file). An activity can start activities of the app (and also from other apps). Each time a new activity starts, the previous activity is stopped, but the system preserves the activity in a stack (Back Stack). Activities can move into the background and then be resumed with their state restored. 3 / 33

Back Stack Activities are arranged in a stack (Back Stack), in the order in which each activity is opened. Activities in the stack are never rearranged, only pushed and popped from the stack. Pushed onto the stack when started by the current activity. Popped off when the user leaves it using the Back button. 4 / 33

Activity States 5 / 33

Activity Lifecycle 6 / 33

Activity Lifetime (I) The entire lifetime of an activity happens between the call to oncreate and the call to ondestroy. Perform setup of global state (such as defining layout) in oncreate, and release all remaining resources in ondestroy. For example, if your activity has a thread running in the background to download data from the network, it might create that thread in oncreate and then stop the thread in ondestroy. The visible lifetime of an activity happens between the call to onstart and the call to onstop. The user can see the activity on-screen and interact with it. The system might call onstart and onstop multiple times during the entire lifetime of the activity, as the activity alternates between being visible and hidden to the user. 7 / 33

Activity Lifetime (II) The foreground lifetime of an activity happens between the call to onresume and the call to onpause. The activity is in front of all other activities on screen and has user input focus. An activity can frequently transition in and out of the foreground For example, onpause is called when the device goes to sleep or when a dialog appears. The code in these two methods should be fairly lightweight in order to avoid slow transitions that make the user wait. 8 / 33

Using lifecycle callbacks (I) Use the oncreate callback to: Initialize the essential components of your activity. (Most important) call setcontentview method to define the layout for the activity. Use the onpause callback to: Stop animations or other ongoing actions that could consume CPU. Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email). Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them. 9 / 33

Using lifecycle callbacks (II) Use onresume callback to: Initialize components that you release during onpause Perform any other initializations that must occur each time the activity enters the Resumed Use onstop callback to: Perform larger, more CPU intensive shut-down operations, such as writing information to a database. 10 / 33

onsaveinstancestate &onrestoreinstancestate These two methods are intended specifically for saving and restoring the dynamic state of an activity. onrestoreinstancestate(bundle savedinstancestate) To this method is passed a Bundle object containing the previous state data. This method is typically used in situations where it makes more sense to restore a previous state after the initialization of the activity has been performed in oncreate and onstart. onsaveinstancestate(bundle outstate) To the method is passed the Bundle object into which the state should be saved and which is subsequently passed through to the oncreate and onrestoreinstancestate methods when the activity is restarted. 11 / 33

12 / 33 UI Events

Handling events and messages The Android framework maintains an event/message queue into which events are placed as they occur. Events/messages are removed from the queue on a first-in, first-out (FIFO) basis. 13 / 33

Event listeners Event Listeners It is an interface in the View class that contains a callback method. Callback method is invoked when the View is triggered by user interaction. Event Listeners Registration It is the process by which an Event Handler gets registered with an Event Listener so that the handler is called when the Event Listener fires the event. Event Handlers It is the callback method that actually handles the event. 14 / 33

15 / 33 Intents

What is an Intent An Intent is a messaging object you can use to request an action from another app component. Intents facilitate communication between components in several ways. 16 / 33

What is in an Intent An Intent object carries information that the Android system uses to determine which component to start, plus information that the recipient component uses in order to properly perform the action. The primary information contained in an Intent is the following: Component name: The name of the component to start. Action: A string that specifies the generic action to perform (such as view or pick). Data: The URI (a Uri object) that references the data to be acted on and/or the MIME type of that data. Category: A string containing additional information about the kind of component that should handle the intent. Extras: Key-value pairs that carry additional information required to accomplish the requested action. Flags: Flags defined in the Intent class that function as metadata for the intent. 17 / 33

Intent Types Implicit intents Do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. For example, if you want to show the user a location on a map, you can use an implicit intent to request that another capable app show a specified location on a map. Explicit intents Specify the component to start by name (the fully-qualified class name). Used to start a component in your own app, because you know the class name of the activity or service you want to start. 18 / 33

Implicit Intents The Android system finds the appropriate component to start by comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers it the Intent object. If multiple intent filters are compatible, the system displays a dialog so the user can pick which app to use. 19 / 33

Explicit intents The system starts the app component specified in the Intent object. Illustration of how an implicit intent is delivered through the system to start another activity: 1. Activity A creates an Intent with an action description and passes it to startactivity(). 2. The Android System searches all apps for an intent filter that matches the intent. When a match is found 3. the system starts the matching activity (Activity B) by invoking its oncreate() method and passing it the Intent. 20 / 33

Intent-Filter (I) < activity android :name =" MainActivity "> <!-- This activity is the main entry, should appear in app launcher --> <intent - filter > < action android :name =" android. intent. action.main " /> < category android :name =" android. intent. category. LAUNCHER " /> </ intent - filter > </ activity > < activity android :name =" ShareActivity "> <!-- This activity handles " SEND " actions with text data --> <intent - filter > < action android :name =" android. intent. action.send "/> < category android :name =" android. intent. category. DEFAULT "/> <data android : mimetype ="text / plain "/> </ intent - filter > <!-- This activity also handles "SEND " and " SEND_MULTIPLE " with media data --> <intent - filter > < action android :name =" android. intent. action.send "/> < action android :name =" android. intent. action. SEND_MULTIPLE "/> < category android :name =" android. intent. category. DEFAULT "/> <data android : mimetype =" application /vnd. google. panorama360 + jpg "/> <data android : mimetype =" image /*"/> <data android : mimetype =" video /*"/> </ intent - filter > </ activity > 21 / 33

Intent-Filter (II) 22 / 33

23 / 33 Practice

Activity lifecycle app (I) 24 / 33

Activity lifecycle app (II) 25 / 33

Working with Intents (I) Passing data to launched Activity 26 / 33

Working with Intents (II) Receiving data from launched Activity 27 / 33

Working with Intents (III) Launching Activity from other App 28 / 33

29 / 33 Assignment

Assignment (I) Create a ListView based app. Each item of the ListView is a string. Functionalities: Add Delete Edit 30 / 33

Assignment (II) Add, implemented as an Option Menu. 31 / 33

Assignment (III) Delete, implemented as a Context Menu. 32 / 33

Assignment (IV) Edit, implemented as a Context Menu. 33 / 33