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

Slides:



Advertisements
Similar presentations
CSE2102 Introduction to Software Engineering Lab 2 Sept/4/2013.
Advertisements

Chapter 6 Jam! Implementing Audio in Android Apps.
Chapter 6: Jam! Implementing Audio in Android Apps.
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.
Android activities 1 CS300. What makes an app?  Activities: presentation layer  Services: invisible workers  Content Providers: databases  Intents:
The Activity Class 1.  One application component type  Provides a visual interface for a single screen  Typically supports one thing a user can do,
A Developer’s Introduction to Google Android Dr. Frank McCown Harding University Fall 2011 This work is licensed under Creative Commons Attribution-NonCommercial.
Android Life Cycle CS328 Dick Steflik. Life Cycle The steps that an application goes through from starting to finishing Slightly different than normal.
More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View.
Android Programming Beomjoo Seo Sep., 12 CS5248 Fall 2012.
Android Application Development 2013 PClassic Chris Murphy 1.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
Intro to Android Programming George Nychis Srinivasan Seshan.
 Understanding an activity  Starting an activity  Passing information between activities  Understanding intents  Understanding the activity lifecycle.
CS5103 Software Engineering Lecture 08 Android Development II.
© Keren Kalif Intro to Android Development Written by Keren Kalif, Edited by Liron Blecher Contains slides from Google I/O presentation.
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.
Software Architecture of Android Yaodong Bi, Ph.D. Department of Computing Sciences University of Scranton.
Android Mobile computing for the rest of us.* *Prepare to be sued by Apple.
Android Info mostly based on Pro Android 3.  User Applications  Java Libraries – most of Java standard edition ◦ Activities/Services ◦ UI/Graphics/View.
Tip Calculator App Building an Android App with Java © by Pearson Education, Inc. All Rights Reserved.
CS378 - Mobile Computing Intents.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
Activities and Intents. Activities Activity is a window that contains the user interface of your application,typically an application has one or more.
Copyright© Jeffrey Jongko, Ateneo de Manila University Of Activities, Intents and Applications.
10/10/2015 E.R.Edwards 10/10/2015 Staffordshire University School of Computing Introduction to Android Overview of Android System Android Components Component.
INTRODUCTION TO ANDROID. Slide 2 Application Components An Android application is made of up one or more of the following components Activities We will.
Overview of Android Application Development
CS378 - Mobile Computing Intents. Allow us to use applications and components that are part of Android System – start activities – start services – deliver.
Understand applications and their components activity service broadcast receiver content provider intent AndroidManifest.xml.
Android – Fragments L. Grewe.
ANDROID L. Grewe Components  Java Standard Development Kit (JDK) (download) (latest version)  AndroidStudio.
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.
1 CMSC 628: Introduction to Mobile Computing Nilanjan Banerjee Introduction to Mobile Computing University of Maryland Baltimore County
CS378 - Mobile Computing Audio.
Activities and Intents Chapter 3 1. Objectives Explore an activity’s lifecycle Learn about saving and restoring an activity Understand intents and how.
School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,
The Ingredients of Android Applications. A simple application in a process In a classical programming environment, the OS would load the program code.
Android Fragments. Slide 2 Lecture Overview Getting resources and configuration information Conceptualizing the Back Stack Introduction to fragments.
INTRODUCTION TO ANDROID. Slide 2 Introduction I take a top-down approach to describing an application’s anatomy.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Putting together the AndroidManifest.xml file Creating multiple.
Lab7 – Appendix.
Mobile Application Development BSCS-7 Lecture # 2
Activity and Fragment.
Android Boot Camp for Developers Using Java, 3E
Activities, Fragments, and Events
Mobile Application Development BSCS-7 Lecture # 6
Activities and Intents
Anatomy of an Android App and the App Lifecycle
Android Mobile Application Development
The Android Activity Lifecycle
Android training in Chandigarh. What is ADB ADB stands for Android Debug Bridge. It is a command line tool that is used to communicate with the emulator.
Anatomy of an Android App and the App Lifecycle
Android Topics Android Activity Lifecycle and Experiment Toast
Activities and Intents
HNDIT2417 Mobile Application Development
Android Programming Tutorial
CIS 470 Mobile App Development
Activities and Intents
Activities and Intents
Mobile Programming Dr. Mohsin Ali Memon.
SE4S701 Mobile Application Development
Activities and Fragments
Android Development Tools
Activities, Fragments, and Intents
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

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

Application Components five primary components different purposes and different lifecycles Activity – single screen with a user interface, app may have several activities, subclass of Activity – Most of early examples will be activities Intents – used to pass information between applications Service – Application component that performs long-running operations in background with no UI – example, an application that automatically responds to texts when driving

Application Components Content Providers – a bridge between applications to share data – for example the devices contacts information – we tend to use these, but not create new ones Broadcast Receivers – component that responds to system wide announcements – battery low, screen off, date changed – also possible to initiate broadcasts from within an application

Hello Android Create an Activity Demonstrate resources created show the Activity lifecycle within the Android OS show the various debugging tools available show how to start one Activity from another

Hello Android Tutorial

Important Files src/HelloAndroid.java – Activity which is started when app executes res/layout/main.xml – Defines & lays out widgets for the activity res/values/strings.xml – String constants used by app gen/R.java (DO NOT MODIFY!) – Auto-generated, auto-updated file with identifiers from main.xml, strings.xml, and elsewhere AndroidManifest.xml – Declares all the app’s components – Names libraries app needs to be linked against – Identifies permissions the app expects to be granted 6

src/HelloAndroid.java Activity which is started when app executes 7

gen/R.java Auto-generated file with identifiers from main.xml, strings.xml, and elsewhere 8 Do not modify!

AndroidManifest.xml Declares all the app’s components Names libraries app needs to be linked against Identifies permissions the app expects to be granted 9 min sdk version

res/layout/main.xml layout of main activity xml view

res/layout/main.xml Drag and Drop UI Editor (your mileage may vary.)

res/layout/main.xml Declares layouts & widgets for the activity 12 Tree from:

Available Layouts 13

Available Widgets 14

res/values/strings.xml String constants used by app Used for supporting Localization – res/values-es/values/strings.xml to support Spanish – res/values-fr/values/strings.xml to support French – Etc. 15

Activity Stack User currently interacting with me Pressing Back or destroying A1 will bring me to the top If Activities above me use too many resources, I’ll be destroyed! Most recently created is at Top Activity 1 Activity 2 Activity 3 Activity N

Typical Game 17 Splash Screen Activity Main Menu Activity Game Play Activity High Scores Activity Settings Activity Conder & Darcey (2010), Fig 4.1, p. 74

Activity Lifecycle

Starting Activities Android applications don't start with a call to main(String[]) instead a series of callback methods are invoked each corresponds to specific stage of the Activity / application lifecycle callback methods also used to tear down Activity / application

Simplified Lifecycle Diagram ready to interact with user

Understanding the Lifecycle Necessary to overload callback methods so you app behaves well: App should not crash if the user receives a phone call or switches to another app while using your app. App should not consume valuable system resources when the user is not actively using it. App should not lose the user's progress if they leave your app and return to it at a later time. App should not crash or lose the user's progress when the screen rotates between landscape and portrait orientation.

Primary States Active – activity is in the foreground and user can interact with it Paused – activity partially obscured by another activity and user cannot interact with it (for example when working with a menu or dialog) Stopped – activity completely hidden and not visible to user. It is in the background. – Activity instance and variables are retained but no code is being executed by the activity Dead, activity terminated (or never started) Two other states, Created and Started, but they are transitory onCreate -> onStart -> onResume

AndroidManifest.xml 23 Specify Activity to start with All Activities that are part of application must be registered in Manifest

What is used for what? Entire lifetime: onCreate / onDestroy – Load UI – Could start and stop threads that should always be running Visible lifetime: onStart / onStop – Access or release resources that influence UI Foreground lifetime: onResume / onPause – Restore state and save state – Start and stop audio, video, animations

LifeCycleTest overload these methods from Activity: – onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), onDestroy() – Use the Log class to log activity – methods: v, d, i, w, e – VERBOSE, DEBUG, INFO, WARN, ERROR – Create a TAG so we can filter

LifeCycleTest Run the app and open the Logcat view. – Eclipse Window-> Show View -> Other -> Android -> Logcat or via DDMS

Logcat After app started

Logcat Rotate emulator with CTRL+F-11

Pausing - onPause method when activity paused you should – stop animations of other CPU intensive tasks – release resources such as broadcast receivers (app stops listening for broadcast info) and handles to sensors such as GPS device or handles to the camera – stop audio and video if appropriate

Stopping - onStop() Many scenarios cause activity to be stopped Well behaved apps save progress and restart seamlessly Activity stopped when: – user performs action in activity that starts another activity in the application – user opens Recent Apps window and starts a new application – user receives phone call use onStop to release all resources and save information (persistence)

How to stop an Activity yourself? Generally, don't worry about it! "Note: In most cases, you should not explicitly finish an activity using these methods. As discussed in the following section about the activity lifecycle, the Android system manages the life of an activity for you, so you do not need to finish your own activities. Calling these methods could adversely affect the expected user experience and should only be used when you absolutely do not want the user to return to this instance of the activity." methods: finish(), finishActivity()

Saving State activities that are paused or stopped the state (instance vars) of the activity are retained – even if not in foreground When activity destroyed the Activity object is destroyed – can save information via onSaveInstanceState method. Write data to Bundle, Bundle given back when restarted

Activity Destruction app may be destroyed under normal circumstances – on its own by calling finish or user pressing the back button to navigate away from app – normal lifecycle methods handle this onPause() -> onStop() -> onDestroy If the system must destroy the activity (to recover resources or on an orientation change) must be able to recreate Activity

Activity Destruction

If Activity destroyed with potential to be recreate later system calls the onSaveInstanceState (Bundle outState) method Bundle is a data structure like a Map – String keys – put methods for primitives, arrays, Strings, Serializables (Java), and Parcels (android)

onSaveInstanceState onRestoreInstanceState() systems write info about views to Bundle other information must be added by programmer – example, board state for mastermind When Activity recreated Bundle sent to onCreate and onRestoreInstanceState() use either method to restore state data / instance variables

Starting You Own Activities You will often start new Activities within your Activity – accomplish a task – get some data Click Button to get name – on button click (look at xml) – create an intent – call startActivityForResult – override onActivityResult() – add new Activity to Manifest – add data to intent, setResult, finish

Intent Demo 38 LifeCycle TestActivity Name Getter Intent holding constant startActivityForResult() Intent holding Name setResult()

Playing Well (or not) With Others The Play Sound button causes a MediaPlayer to be created and plays a sound The Lifecycle app does not clean up after itself If app destroyed MediaPlayer keeps playing!!

References Android Introduction by Marko Gargenta, roid-software-platform/ roid-software-platform/ Android Dev Guide undamentals.html undamentals/activities.html undamentals.html undamentals/activities.html Pro Android by Hashimi & Komatineni (2009) Frank McCown, Harding University