Applications with Multiple Activities. Most applications will have more than one activity. The main activity is started when the application is started.

Slides:



Advertisements
Similar presentations
Application Fundamentals Android Development. Announcements Posting in D2L Tutorials.
Advertisements

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.
The Activity Class 1.  One application component type  Provides a visual interface for a single screen  Typically supports one thing a user can do,
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
@2011 Mihail L. Sichitiu1 Android Introduction Communication between Activities.
Data Storage: Part 1 (Preferences)
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
Better reference the original webpage :
 Understanding an activity  Starting an activity  Passing information between activities  Understanding intents  Understanding the activity lifecycle.
Favorite Twitter® Searches App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Networking: Part 2 (Accessing the Internet). The UI Thread When an application is launched, the system creates a “main” UI thread responsible for handling.
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.
Copyright© Jeffrey Jongko, Ateneo de Manila University Of Activities, Intents and Applications.
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.
Networking: Part 1 (Web Content). Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine.
Cosc 5/4730 Android Communications Intents, callbacks, and setters.
Linking Activities using Intents How to navigate between Android Activities 1Linking Activities using Intents.
Services A Service is an application component that can perform long-running operations in the background and does not provide a user interface. An application.
Copyright© Jeffrey Jongko, Ateneo de Manila University Basic Views and Layouts.
Activity Android Club Agenda Hello Android application Application components Activity StartActivity.
Mobile Programming Midterm Review
New Activity On Button Click via Intent. App->res->Layout->Blank Activity A new xml file is created and a new class is added to java folder. In manifest.xml.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to.
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.
Services Background operating component without a visual interface Running in the background indefinitely Differently from Activity, Service in Android.
Lecture 2: Android Concepts
Technische Universität München Services, IPC and RPC Gökhan Yilmaz, Benedikt Brück.
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.
David Sutton SMS TELEPHONY IN ANDROID. OUTLINE  This week’s exercise, an SMS Pub Quiz  Simulating telephony on an emulator  Broadcast Intents and broadcast.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Putting together the AndroidManifest.xml file Creating multiple.
Lab7 – Appendix.
Lab7 – Advanced.
Permissions.
Intents and Broadcast Receivers
Lecture 2: Android Concepts
Activity and Fragment.
several communicating screens
Linking Activities using Intents
Android – Event Handling
ListView: Part 2.
Communication between Activities
CS499 – Mobile Application Development
Activities and Intents
The Android Activity Lifecycle
Android Introduction Camera.
Android Programming Lecture 9
Android Programming Lecture 5
CIS 470 Mobile App Development
Many thanks to Jun Bum Lim for his help with this tutorial.
Activities and Intents
Activities and Intents
Activities and Intents
CIS 470 Mobile App Development
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
Lecture 2: Android Concepts
Objects First with Java
Activities and Fragments
Chapter 5 Your Second Activity.
Activities, Fragments, and Intents
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

Applications with Multiple Activities

Most applications will have more than one activity. The main activity is started when the application is started. The main activity can launch another activity, usually in response to some event such as a button click. The launching of another activity causes the main activity to pause while the second activity is active. When the second activity finishes, the main activity is brought to the foreground and resumed. Slide 2©SoftMoore Consulting

Declaring Activities in the Android Manifest Every activity must be declared in AndroidManifest.xml. <activity android:name=".MainActivity" > <activity android:name=".Activity2" > Slide 3©SoftMoore Consulting (the main activity) (a second activity) Also true for services and content providers. Broadcast receivers can be declared in the manifest or created/registered dynamically.

Forcing Single Task Mode It is possible to navigate away from an activity and then relaunch it again, leading to multiple instances of the activity on the device. Eventually the redundant instances of the activity are killed to free up memory, but in the meantime, their existence can be confusing. To insure that only one instance of an activity runs on the device, for any activity that has MAIN and LAUNCHER intent filters, modify the activity element in AndroidManifest.xml to add the following attribute: android:launchMode="singleInstance" Slide 4©SoftMoore Consulting

Using Intents Use an intent to launch another application component such as an activity or a service. Recall that an explicit intent specifies the component to start by name. An explicit intent is typically used to start a component within the same application. Slide 5©SoftMoore Consulting

Example: Using an Explicit Intent to Launch a Second Activity // in the onCreate() method for MainActivity Button activity1Button = (Button)findViewById(R.id.activity1Button); activity1Button.setOnClickListener(new View.OnClickListener() public void onClick(View v) { Intent intent = new Intent(MainActivity.this, Activity2.class); startActivity(intent); } }); Slide 6©SoftMoore Consulting

Designing Applications with Multiple Activities If the main activity launches a second activity using an intent, then the second activity should not normally re-launch the main activity using an intent. This would cause multiple instances of the main activity to exist on the activity stack. Instead, the second activity would normally call its finish() method to return to the main activity, usually in response to some event such as a button click within the second activity. Slide 7©SoftMoore Consulting

Example: Returning to the Main Activity // in the onCreate() method for Activity2 Button activity2Button = (Button)findViewById(R.id.activity2Button); activity2Button.setOnClickListener(new View.OnClickListener() public void onClick(View v) { finish(); } }); Slide 8©SoftMoore Consulting

Using Intents for Inter-Activity Communication Extras are key-value pairs that provide additional information to the component handling the intent. When one activity launches a second activity, the first activity can attach “extra” data to the Intent used to invoke the second activity in a manner somewhat analogous to passing parameters in a method call. When the second activity finishes, it can attach “extra” data to an Intent and return it back to the first activity in a manner somewhat analogous to methods returning values. Slide 9©SoftMoore Consulting

Attaching Extra Data to an Intent Selected “put” methods Intent putExtra(String name, boolean value) Intent putExtra(String name, double value) Intent putExtra(String name, int value) Intent putExtra(String name, Serializable value) Intent putExtra(String name, String value) Selected “get” methods boolean getBooleanExtra(String name, boolean defaultValue) double getDoubleExtra(String name, double defaultValue) int getIntExtra(String name, int defaultValue) Serializable getSerializableExtra(String name) String getStringExtra(String name) Slide 10©SoftMoore Consulting

Attaching Extra Data to an Intent (continued) There are array versions of each of the above methods; e.g. Intent putExtra(String name, boolean[] value) Intent putExtra(String name, String[] value) Slide 11©SoftMoore Consulting

Launching a New Activity The method startActivity() can be used to launch a new activity when no result is to be returned. The method startActivityForResult() can be used to launch a new activity for which you would like a result when it has finished. When using startActivityForResult(), you should also implement method onActivityResult() to retrieve the “extras” returned from the launched activity. Slide 12©SoftMoore Consulting

Example: Launching an Activity for a Result Slide 13©SoftMoore Consulting

Example: Launching an Activity for a Result (in class MainActivity ) private static final int EDIT_ACTIVITY = 1; private String name = "John Moore";... // in the onCreate() method Button mainActivityButton = (Button) findViewById(R.id.mainActivityButton); mainActivityButton.setOnClickListener(new View.OnClickListener() public void onClick(View v) { Intent intent = new Intent(MainActivity.this, Activity2.class); intent.putExtra("name", name); startActivityForResult(intent, EDIT_ACTIVITY); } }); Slide 14©SoftMoore Consulting second integer parameter identifies the call

Retrieving Extras from an Intent // in the onCreate() method of Activity2 Intent intent = getIntent(); String name = intent.getStringExtra("name"); EditText editText = (EditText) findViewById(R.id.editText); editText.setText(name); Slide 15©SoftMoore Consulting

Screen for Activity2 Slide 16©SoftMoore Consulting

Another Screen for Activity2 Slide 17©SoftMoore Consulting

Returning an Intent as a Result (in class Activity2 ) Button submitButton = (Button) findViewById(R.id.submitButton); submitButton.setOnClickListener(new View.OnClickListener() public void onClick(View v) { EditText editText = (EditText) findViewById(R.id.editText); Intent intent = new Intent(); intent.putExtra("name", editText.getText().toString()); setResult(RESULT_OK, intent); finish(); } }); Slide 18©SoftMoore Consulting

Example: Retrieving Extras from a Result (in class MainActivity protected void onActivityResult(int requestCode, int resultCode, Intent intent) { if (requestCode == EDIT_ACTIVITY && resultCode == RESULT_OK) { name = intent.getStringExtra("name"); TextView textView = (TextView) findViewById(R.id.mainActivityTextView); textView.setText(name); } Slide 19©SoftMoore Consulting second integer parameter from the original call

Example: Retrieving Extras from a Result (continued) Slide 20©SoftMoore Consulting

Relevant Links Opening a New Screen Starting an Activity Slide 21©SoftMoore Consulting