Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Putting together the AndroidManifest.xml file Creating multiple.

Slides:



Advertisements
Similar presentations
CE881: Mobile and Social Application Programming Simon M. Lucas Quiz, Walkthrough, Exercise, Lifecycles, Intents.
Advertisements

Programming with Android: Activities and Intents Luca Bedogni Marco Di Felice Dipartimento di Informatica – Scienza e Ingegneria Università di Bologna.
Cosc 5/4730 Android Services. What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what.
Android 02: Activities David Meredith
The Android Activity Lifecycle. Slide 2 Introduction Working with the Android logging system Rotation and multiple layouts Understanding the Android activity.
Manifest File, Intents, and Multiple Activities. Manifest File.
Mobile Programming Pertemuan 6 Presented by Mulyono Poltek NSC Surabaya.
Intent An Intent describes the operation to be performed. Intents are used to start an activity using either of the following methods – Context.startActivity()
ANDROID PROGRAMMING MODULE 1 – GETTING STARTED
INTERNATIONAL SUMMER ACADEMIC COURSE UNIVESITY OF NIS ISAC – Android programming.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
CS378 - Mobile Computing Anatomy of an Android App and the App Lifecycle.
Better reference the original webpage :
 Understanding an activity  Starting an activity  Passing information between activities  Understanding intents  Understanding the activity lifecycle.
© Keren Kalif Intro to Android Development Written by Keren Kalif, Edited by Liron Blecher Contains slides from Google I/O presentation.
Chapter 5: Investigate! Lists, Arrays, and Web Browsers.
Favorite Twitter® Searches App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Hello world Follow steps under the sections “Create an AVD” and “Create a New Android Project” at
CS378 - Mobile Computing Intents.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
INTRODUCTION TO ANDROID. Slide 2 Application Components An Android application is made of up one or more of the following components Activities We will.
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.
COMP 365 Android Development.  Every android application has a manifest file called AndroidManifest.xml  Found in the Project folder  Contains critical.
Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Lec 03 Intents Explicit Intents Implicit Intents.
Cosc 5/4730 Android Communications Intents, callbacks, and setters.
Linking Activities using Intents How to navigate between Android Activities 1Linking Activities using Intents.
First Venture into the Android World Chapter 1 Part 2.
1 CMSC 628: Introduction to Mobile Computing Nilanjan Banerjee Introduction to Mobile Computing University of Maryland Baltimore County
Applications with Multiple Activities. Most applications will have more than one activity. The main activity is started when the application is started.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to.
Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (
Introducing Intents Intents Bind application components and navigate between them Transform device into collection of interconnected systems Creating a.
Intents 1 CS440. Intents  Message passing mechanism  Most common uses:  starting an Activity (open an , contact, etc.)  starting an Activity.
Android Intents Nasrullah. Using the application context You use the application context to access settings and resources shared across multiple activity.
Lecture 2: Android Concepts
Activities and Intents Chapter 3 1. Objectives Explore an activity’s lifecycle Learn about saving and restoring an activity Understand intents and how.
Intents and Broadcast Receivers Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution.
The Ingredients of Android Applications. A simple application in a process In a classical programming environment, the OS would load the program code.
INTRODUCTION TO ANDROID. Slide 2 Introduction I take a top-down approach to describing an application’s anatomy.
Editing a Twitter search. Viewing search results in a browser.
Chapter 5: Investigate! Lists, Arrays, and Web Browsers.
CS371m - Mobile Computing Intents 1. Allow us to use applications and components that are already part of Android System – start activities – start services.
Intents and Broadcast Receivers
Lecture 2: Android Concepts
Android 5: Interacting with Other Apps
Linking Activities using Intents
Activities and Intents
Anatomy of an Android App and the App Lifecycle
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
The Android Activity Lifecycle
CIS 470 Mobile App Development
Anatomy of an Android App and the App Lifecycle
Activities and Intents
Activities and Intents
Activities and Intents
CIS 470 Mobile App Development
Objects First with Java
Activities and Intents
It is used to Start an Activity Start a Service Deliver a Broadcast
Mobile Programming Dr. Mohsin Ali Memon.
Lecture 2: Android Concepts
Objects First with Java
Mobile Programming Dr. Mohsin Ali Memon.
Activities and Fragments
Chapter 5 Your Second Activity.
Preference Activity class
Activities, Fragments, and Intents
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

Working with Multiple Activities

Slide 2 Introduction Working with multiple activities Putting together the AndroidManifest.xml file Creating multiple views Introduction to intents Passing data to activities

Slide 3 Multiple Activities (Introduction) When the user switches between activities, one activity is stopped, and the other is started Remember the activity lifecycle Note that you can start activities that belong to other applications A Web Browser for example A mailer A dialer

Slide 4 Starting an Activity To start a new activity, you call startActivity() passing an Intent as the argument The Intent describes the activity that you want to start The user can then select from the possible intents (external) Or the activity just runs (external) An activity can also return results

Slide 5 Creating a Second Activity Remember that an activity is just a class So we just Add a new class Inherit from android.app.Activity Add the activity to the manifest

Slide 6 Creating a Second Activity

Slide 7 Add to Manifest

Slide 8 Resulting Manifest

Slide 9 Creating a Layout You have created portrait and landscape layouts When working with multiple activities, you need to create layouts for those activities In the project Explorer, click New, Other

Slide 10 Creating a Layout (Illustration) In the first dialog, select Android XML Layout File In the second dialog, select the desired layout

Slide 11 Creating a Layout (Illustration) Set configuration options Note the file is added to the res/layout folder by default

Slide 12 Introduction to Intents Before we can explicitly start an activity, you need to understand intents An intent is an object used to communicate with the OS Intents are used with activities, services, broadcast receivers, and content providers We have only discussed activities so far You can use intents to tell the Android OS which activity to start

Slide 13 Intents (Philosophically) As the name implies, it’s an intention to do an action It’s a message to say I did something I want something to happen When you create an intent, you are saying that you want to move from one activity to another

Slide 14 Intents (Types) There are two types of intents Explicit intents are used with a Context and Class object to start an activity within your application Implicit intents are used to start activities outside of your application THERE ARE MUCH MORE TO INTENTS THAN DISCUSSED HERE

Slide 15 Creating an Implicit Intent On overloaded version of the Intent constructor accepts an action Intent.ACTION_VIEW is a generalized intent The application is inferred based on the data passed when the activity is started Intent.ACTION_CALL starts the default dialer

Slide 16 Creating an Implicit Intent (Example) Create an implicit intent and pass a URL to the intent String url = " Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i);

Slide 17 Intent (Constructor) This is but one of the many constructors The first argument contains the current activity The second argument contains the class name of the activity to be started

Slide 18 Intent (Creating) The following appears in the current activity (MainActivity.this) and starts the second activity (Page2Activity.class)

Slide 19 Starting an Activity (Illustration) startActivity is called with an Intent. The ActivityManager uses that intent to determine the activity to start

Slide 20 Passing Data to Activities You can pass data from one activity to anther through intent extras Extras are just arbitrary data that can be passed to an intent An extra is a key / value pair that is passed to an intent The started activity can read this extra data

Slide 21 Creating (Writing) an Extra First, create the Intent as shown before Second, call putExtra() First argument contains the key Second argument contains the value

Slide 22 Creating (Writing) an Extra The first argument contains the key, which here is a constant “LastActivity” The second contains the value “MainActivity”

Slide 23 Reading an Extra The getIntent() method returns an Intent object It’s methods get values of a particular type getBooleanExtra(), getByteExtra(), getIntExtra(), etc…

Slide 24 Reading an Extra (Example) Read an extra into a TextView

Slide 25 Reading and Writing Instance Data (1) By default, the system uses the Bundle instance to save information about each View object Layout state information is restored automatically You are responsible for saving and restoring other instance data Note that a rotation causes the application to be destroyed and recreated with a new layout (later)

Slide 26 Reading and Writing Instance Data (2) To save additional instance data, override the onSaveInstanceState() callback method To restore the saved instance data, override the onRestoreInstanceState() callback method

Slide 27 onSaveInstanceState() Call putInt() (or other type) to save data The first argument contains the key The second argument contains the value

Slide 28 onRestoreInstanceState() Call getInt() (or other type) to restore data. The first argument contains the key The method returns the value

Slide 29 State (Illustration)

Slide 30 Returning an Activity Result (Introduction) There are times when we need to return a result from an activity We start an activity as before, however, pass a request code when starting the activity The request code is an integer It is send to the child (started) activity The result is then returned to the parent

Slide 31 Returning an Activity Result Call startActivityForResult() instead of startActivity() The first argument contains the intent The second argument contains the integer result that will be returned The result is returned through overriding onActivityResult() in the parent activity

Slide 32 Returning an Activity Result To return the result, you call one of two forms of setResult() The first returns a result code Typically Activity.RESULT_CANCELED Activity.RESULT_OK The second returns the result code and intent data Call finish() to return from the activity

Slide 33 Activity Result Example (1) Start the activity as before with startActivity except call startActivityForResult The first argument contains the activity as before The second argument contains an integer request code It answers the question, from which activity are we returning

Slide 34 Activity Result Example (2) Here we return a code but no data The intent is empty and has no extras RESULT_CANCELED is an Android constant We call finish to return to the calling Activity

Slide 35 Activity Result Example (2a) Here we return a code and data The Intent has extra data We change the return code

Slide 36 Activity Result Example (3) In the calling activity, handle the onActivityResult() event

Slide 37 AndroidManifest.xml (Multiple Activities) The AndroidManifest.xml must list all of the activities Remember one activity is designated as the launcher activity

Slide 38 AndroidManifest.xml (Multiple Activities)

Slide 39 Implicit Activities (Introduction) Implicit activities are handled outside of your application They are other registered applications Your browser, mailer, or dialer

Slide 40 Creating an Implicit Intent Creating an implicit intent is a two step process. Create an intent with an argument describing the intent category Intent.ACTION_VIEW is a generalized intent commonly used to process URLS Intent.ACTION_CALL invokes a dailoger On the Intent, Call the setData method to pass the url Call startActivity as usual

Slide 41 Creating an Implicit Intent Example to visit UNR (more about URIs later when we talk about the network) String urlString = " Intent i = new Intent(Intent.ACTION_VIEW); Uri u = Uri.parse(urlString); i.setData(u); startActivity(i);