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

Similar documents
UNDERSTANDING ACTIVITIES

LECTURE NOTES OF APPLICATION ACTIVITIES

Lab 1: Getting Started With Android Programming

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

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

States of Activities. Active Pause Stop Inactive

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

Overview of Activities

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

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

Lifecycle Callbacks and Intents

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

Minds-on: Android. Session 2

Application Fundamentals

Android Activities. Akhilesh Tyagi

Vienos veiklos būsena. Theory

Embedded Systems Programming - PA8001

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

COLLEGE OF ENGINEERING, NASHIK-4

Introduction to Android development

Android Fundamentals - Part 1

Real-Time Embedded Systems

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

Programming Mobile Applications with Android Lab2

Lifecycle-Aware Components Live Data ViewModel Room Library

ECOM 5341 Mobile Computing(Android) Eng.Ruba A. Salamah

Introduction to Android

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

CMSC436: Fall 2013 Week 3 Lab

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

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

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

MODULE 2: GETTING STARTED WITH ANDROID PROGRAMMING

Android Basics. Android UI Architecture. Android UI 1

Android Ecosystem and. Revised v4presenter. What s New

Android Application Development

Activities. Repo:

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

Understanding Application

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

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

BCA 6. Question Bank

UI Fragment.

INTRODUCTION TO ANDROID

Activities and Fragments

FRAGMENTS.

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

Programming Android UI. J. Serrat Software Design December 2017

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

COSC 3P97 Mobile Computing

CS 4330/5390: Mobile Application Development Exam 1

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

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

EMBEDDED SYSTEMS PROGRAMMING Application Basics

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

Android Basics. - Bhaumik Shukla Android Application STEALTH FLASH

application components

Lab 3: Using Worklight Server and Environment Optimization Lab Exercise

Android Programming Lecture 2 9/7/2011

Lecture 7: Data Persistence : shared preferences. Lecturer : Ali Kadhim Al-Bermani Mobile Fundamentals and Programming

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

Questions and Answers. Q.1) Which of the following is the most ^aeuroeresource hungry ^aeuroepart of dealing with activities on android?

Configuring the Android Manifest File

Libraries are wri4en in C/C++ and compiled for the par>cular hardware.

Android Programmierung leichtgemacht. Lars Vogel

By Ryan Hodson. Foreword by Daniel Jebaraj

University of Stirling Computing Science Telecommunications Systems and Services CSCU9YH: Android Practical 1 Hello World

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

CE881: Mobile & Social Application Programming

Multiple Activities. Many apps have multiple activities

Figure 2.10 demonstrates the creation of a new project named Chapter2 using the wizard.

CS 403X Mobile and Ubiquitous Computing Lecture 3: Introduction to Android Programming Emmanuel Agu

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

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

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

Introduction To Android

Xin Pan. CSCI Fall

Basic Android Setup for Machine Vision Fall 2015

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

12 Publishing Android Applications

Android framework overview Activity lifecycle and states

Computer Science E-76 Building Mobile Applications

Introduction To JAVA Programming Language

Mobile Computing. Introduction to Android

Programming in Android. Nick Bopp

Mobile User Interfaces

ES E 3 3 L a L b 5 Android development

Course Learning Outcomes (CLO): Student Outcomes (SO):

Fragments and the Maps API

Static Detection of Energy Defect Patterns in Android Applications

Google Maps Troubleshooting

Answers to Exercises

Lab 1 - Setting up the User s Profile UI

Android Overview. Most of the material in this sec5on comes from h6p://developer.android.com/guide/

CS 528 Mobile and Ubiquitous Computing Lecture 1b: Introduction to Android. Emmanuel Agu

MARS AREA SCHOOL DISTRICT Curriculum TECHNOLOGY EDUCATION

Introduction to Android

Group B: Assignment No 8. Title of Assignment: To verify the operating system name and version of Mobile devices.

Transcription:

Activities Ref: Wei-Meng Lee, BEGINNING ANDROID 4 APPLICATION DEVELOPMENT, Ch2, John Wiley & Sons, 2012 An application can have zero or more activities. Typically, applications have one or more activities; and the main purpose of an activity is to interact with the user. From the moment an activity appears on the screen to the moment it is hidden, it goes through a number of stages, known as an activity s life cycle. Understanding the life cycle of an activity is vital to ensuring that your application works correctly. Apart from activities, another unique concept in Android is that of an intent. An intent is basically the glue that enables different activities from different applications to work together seamlessly, ensuring that tasks can be performed as though they all belong to one single application. 1. Understanding Activities To create an activity, you create a Java class that extends the Activity base class: Your activity class loads its UI component using the XML fi le defined in your res/layout folder. In this example, you would load the UI from the main.xml file: setcontentview(r.layout.main); Every activity you have in your application must be declared in your AndroidManifest.xml file, like this: 1

Within the definition for this activity, there is an element named <intent-filter>: The action for the intent filter is named android.intent.action.main to indicate that this activity serves as the entry point for the application. The category for the intent-filter is named android.intent.category.launcher to indicate that the application can be launched from the device s launcher icon. The Activity base class defines a series of events that govern the life cycle of an activity. The Activity class defines the following events: oncreate() Called when the activity is first created onstart() Called when the activity becomes visible to the user onresume() Called when the activity starts interacting with the user onpause() Called when the current activity is being paused and the previous activity is being resumed onstop() Called when the activity is no longer visible to the user ondestroy() Called before the activity is destroyed by the system (either manually or by the system to conserve memory) onrestart() Called when the activity has been stopped and is restarting again. 2

3

Please See the code on your Text Book : Ch2 : Page 38. The code uses Log to printout the results on debugging window in Eclips editor. When the activity is first loaded, you should see something very similar to the following in the LogCat window 11-16 06:25:59.396: D/Lifecycle(559): In the oncreate() event 11-16 06:25:59.396: D/Lifecycle(559): In the onstart() event 11-16 06:25:59.396: D/Lifecycle(559): In the onresume() event If you click the Back button on the Android emulator, the following is printed: 11-16 06:29:26.665: D/Lifecycle(559): In the onpause() event 11-16 06:29:28.465: D/Lifecycle(559): In the onstop() event 11-16 06:29:28.465: D/Lifecycle(559): In the ondestroy() event Click the Home button and hold it there. Click the Activities icon and observe the following: 11-16 06:31:08.905: D/Lifecycle(559): In the oncreate() event 11-16 06:31:08.905: D/Lifecycle(559): In the onstart() event 11-16 06:31:08.925: D/Lifecycle(559): In the onresume() event Click the Phone button on the Android emulator so that the activity is pushed to the background. Observe the output in the LogCat window: 11-16 06:32:00.585: D/Lifecycle(559): In the onpause() event 11-16 06:32:05.015: D/Lifecycle(559): In the onstop() event Notice that the ondestroy() event is not called, indicating that the activity is still in memory. Exit the phone dialer by clicking the Back button. The activity is now visible again. Observe the output in the LogCat window: 11-16 06:32:50.515: D/Lifecycle(559): In the onrestart() event 11-16 06:32:50.515: D/Lifecycle(559): In the onstart() event 11-16 06:32:50.515: D/Lifecycle(559): In the onresume() event The onrestart() event is now fi red, followed by the onstart() and onresume() methods. 4

From the preceding example, you can derive the following guidelines: Use the oncreate() method to create and instantiate the objects that you will be using in your application. Use the onresume() method to start any services or code that needs to run while your activity is in the foreground. Use the onpause() method to stop any services or code that does not need to run when your activity is not in the foreground. Use the ondestroy() method to free up resources before your activity is destroyed. 2. LINKING ACTIVITIES USING INTENTS An Android application can contain zero or more activities. When your application has more than one activity, you often need to navigate from one to another. In Android, you navigate between activities through what is known as an intent. The best way to understand this very important but somewhat abstract concept in Android is to experience it first hand and see what it helps you to achieve. The following code shows how to add another activity to an existing project and then navigate between the two activities. Please See full code on your Text Book : Ch2 : Page 54. Or you can use this simple method to create and add new activity. : 1. Using Eclipse, make new Project. 2. Name your default activity : UsingIntentActivity 5

3. Right-click on the package name on the scr folder select New Other Android Android Activity. then follow the wizard instructions. name it secondactivity. 4. You can change its title by going to xml file and change this : 5. Create new button : 6. Modify the SecondActivity.java file as shown in bold: As you have learned, an activity is made up of a UI component (for example, main.xml) and a class component (for example, UsingIntentActivity.java). Hence, if you want to add another activity to a project, you need to create these two components. In the AndroidManifest.xml file, specifically you have added the following: 6

Here, you have added a new activity to the application. Note the following: The name (class) of the new activity added is SecondActivity. The label for the new activity is named Second Activity. The intent filter name for the new activity is net.learn2develop.secondactivity. Other activities that wish to call this activity will invoke it via this name. Ideally, you should use the reverse domain name of your company as the intent filter name in order to reduce the chances of another application having the same intent filter name. The category for the intent filter is android.intent.category.default. You need to add this to the intent filter so that this activity can be started by another activity using the startactivity() method. When the button is clicked, you use the startactivity() method to display SecondActivity by creating an instance of the Intent class and passing it the intent filter name of SecondActivity (which is net.learn2develop.secondactivity): Activities in Android can be invoked by any application running on the device. For example, you can create a new Android project and then display SecondActivity by using its net.learn2develop.secondactivity intent filter. This is one of the fundamental concepts in Android that enables an application to invoke another easily. If the activity that you want to invoke is defined within the same project, you can rewrite the preceding statement like this: 7