SlideShare a Scribd company logo
1 of 47
Download to read offline
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities 1
1
Activities and
Intents
1
Android Developer Fundamentals
Lesson 2
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
2.1 Activities
2
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Contents
3
● Activities
● Defining an activity
● Starting a new activity with an intent
● Passing data between activities with extras
● Navigating between activities
Android Developer Fundamentals
Activities
(high-level view)
4
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
What is an Activity?
5
● An Activity is an application component
● Represents one window, one hierarchy of views
● Typically fills the screen, but can be embedded in other
activity or a appear as floating window
● Java class, typically one activity in one file
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
What does an Activity do?
6
● Represents an activity, such as ordering groceries, sending
email, or getting directions
● Handles user interactions, such as button clicks, text entry,
or login verification
● Can start other activities in the same or other apps
● Has a life cycle—is created, started, runs, is paused,
resumed, stopped, and destroyed
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Examples of activities
7
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Apps and activities
8
● Activities are loosely tied together to make up an app
● First activity user sees is typically called "main activity"
● Activities can be organized in parent-child relationships in
the Android manifest to aid navigation
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Layouts and Activities
● An activity typically has a UI layout
● Layout is usually defined in one or more XML files
● Activity "inflates" layout as part of being created
9
Android Developer Fundamentals
Implementing
Activities
10
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Implement new activities
11
1. Define layout in XML
2. Define Activity Java class
○ extends AppCompatActivity
3. Connect Activity with Layout
○ Set content view in onCreate()
4. Declare Activity in the Android manifest
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
1.Define layout in XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Let's Shop for Food!" />
</RelativeLayout>
12
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
2. Define Activity Java class
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
13
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
3. Connect activity with layout
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
14
in this XML file
is layout
Resource
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
4. Declare activity in Android manifest
<activity android:name=".MainActivity">
15
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
4. Declare main activity in manifest
Main Activity needs to include intent to start from launcher icon
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
16
Android Developer Fundamentals
Intents
17
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
What is an intent?
18
An intent is a description of an operation to be performed.
An Intent is an object used to request an action from another
app component via the Android system.
App component
Originator
Intent Action
Android
System
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
What can intents do?
19
● Start activities
○ A button click starts a new activity for text entry
○ Clicking Share opens an app that allows you to post a photo
● Start services
○ Initiate downloading a file in the background
● Deliver broadcasts
○ The system informs everybody that the phone is now charging
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Explicit and implicit intents
20
Explicit Intent
● Starts a specific activity
○ Request tea with milk delivered by Nikita
○ Main activity starts the ViewShoppingCart activity
Implicit Intent
● Asks system to find an activity that can handle this request
○ Find an open store that sells green tea
○ Clicking Share opens a chooser with a list of apps
Android Developer Fundamentals
Starting
Activities
21
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Start an Activity with an explicit intent
To start a specific activity, use an explicit intent
1. Create an intent
○ Intent intent = new Intent(this, ActivityName.class);
2. Use the intent to start the activity
○ startActivity(intent);
22
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Start an Activity with implicit intent
To ask Android to find an Activity to handle your request, use
an implicit intent
1. Create an intent
○ Intent intent = new Intent(action, uri);
2. Use the intent to start the activity
○ startActivity(intent);
23
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Implicit Intents - Examples
24
Show a web page
Uri uri = Uri.parse("http://www.google.com");
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
Dial a phone number
Uri uri = Uri.parse("tel:8005551234");
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
How Activities Run
● All activities are managed by the Android runtime
● Started by an "intent", a message to the Android runtime to
run an activity
25
MainActivity
What do you want to do?
FoodListActivity
Choose food items...Next
OrderActivity
Place order
User clicks
launcher icon
Android
System
Intent: Start app Start main
activity Android
System
Start choose
food activity Android
System
Start finish
order activity
Intent: Shop Intent: order
Android Developer Fundamentals
Sending and
Receiving Data
26
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Two types of sending data with intents
● Data—one piece of information whose data location can
be represented by an URI
● Extras—one or more pieces of information as a collection
of key-value pairs in a Bundle
27
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Sending and retrieving data
In the first (sending) activity:
1. Create the Intent object
2. Put data or extras into that intent
3. Start the new activity with startActivity()
In the second (receiving) activity,:
1. Get the intent object the activity was started with
2. Retrieve the data or extras from the Intent object
28
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Putting a URI as intent data
// A web page URL
intent.setData(
Uri.parse("http://www.google.com"));
// a Sample file URI
intent.setData(
Uri.fromFile(new File("/sdcard/sample.jpg")));
29
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Put information into intent extras
● putExtra(String name, int value)
⇒ intent.putExtra("level", 406);
● putExtra(String name, String[] value)
⇒ String[] foodList = {"Rice", "Beans", "Fruit"};
intent.putExtra("food", foodList);
● putExtras(bundle);
⇒ if lots of data, first create a bundle and pass the bundle.
● See documentation for all
30
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Sending data to an activity with extras
public static final String EXTRA_MESSAGE_KEY =
"com.example.android.twoactivities.extra.MESSAGE";
Intent intent = new Intent(this, SecondActivity.class);
String message = "Hello Activity!";
intent.putExtra(EXTRA_MESSAGE_KEY, message);
startActivity(intent);
31
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Get data from intents
● getData();
⇒ Uri locationUri = intent.getData();
● int getIntExtra (String name, int defaultValue)
⇒ int level = intent.getIntExtra("level", 0);
● Bundle bundle = intent.getExtras();
⇒ Get all the data at once as a bundle.
● See documentation for all
32
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Returning data to the starting activity
1. Use startActivityForResult() to start the second activity
2. To return data from the second Activity:
● Create a new Intent
● Put the response data in the Intent using putExtra()
● Set the result to Activity.RESULT_OK
or RESULT_CANCELED, if the user cancelled out
● call finish() to close the activity
1. Implement onActivityResult() in first activity
33
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
startActivityForResult()
startActivityForResult(intent, requestCode);
● Starts activity (intent), assigns it identifier (requestCode)
● Returns data via intent extras
● When done, pop stack, return to previous activity, and execute
onActivityResult() callback to process returned data
● Use requestCode to identify which activity has "returned"
34
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
1.startActivityForResult() Example
public static final int CHOOSE_FOOD_REQUEST = 1;
Intent intent = new Intent(this, ChooseFoodItemsActivity.class);
startActivityForResult(intent, CHOOSE_FOOD_REQUEST);
35
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
2.Return data and finish second activity
// Create an intent
Intent replyIntent = new Intent();
// Put the data to return into the extra
replyIntent.putExtra(EXTRA_REPLY, reply);
// Set the activity's result to RESULT_OK
setResult(RESULT_OK, replyIntent);
// Finish the current activity
finish();
36
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
3.Implement onActivityResult()
public void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TEXT_REQUEST) { // Identify activity
if (resultCode == RESULT_OK) { // Activity succeeded
String reply = data.getStringExtra(SecondActivity.EXTRA_REPLY);
// … do something with the data
}}}
37
Android Developer Fundamentals
Navigation
38
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Activity stack
● When a new activity is started, the previous activity is
stopped and pushed on the activity back stack
● Last-in-first-out-stack—when the current activity ends, or the
user presses the Back button, it is popped from the
stack and the previous activity resumes
39
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Activity Stack
40
MainActivity
What do you want to do?
FoodListActivity
Choose food items
CartActivity
View shopping cart
MainActivity
What do you want to do?
FoodListActivity
Choose food items
MainActivity
What do you want to do?
FoodListActivity
Choose food items
CartActivity
View shopping cart
OrderActivity
Place order
MainActivity
What do you want to do?
After viewing shopping
cart, user decides to add
more items, then places
order.
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Two forms of navigation
Temporal or back navigation
● provided by the device's back button
● controlled by the Android system's back stack
Ancestral or up navigation
● provided by the app's action bar
● controlled by defining parent-child relationships
between activities in the Android manifest
41
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Back navigation
● Back stack preserves history of recently viewed screens
● Back stack contains all the activities that have been launched by the
user in reverse order for the current task
● Each task has its own back stack
● Switching between tasks activates that task's back stack
● Launching an activity from the home screen starts a new task
● Navigate between tasks with the overview or recent tasks screen
42
Android Developer Fundamentals
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Up navigation
43
● Goes to parent of current activity
● Define an activity's parent in Android manifest
● Set parentActivityName
<activity
android:name=".ShowDinnerActivity"
android:parentActivityName=".MainActivity" >
</activity>
Android Developer Fundamentals
Learn more
44
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
Learn more
● Android Application Fundamentals
● Starting Another Activity
● Activity (API Guide)
● Activity (API Reference)
● Intents and Intent Filters (API Guide)
● Intent (API Reference)
● Navigation
45
Android Developer Fundamentals
This work is licensed under a Creative
Commons Attribution-NonCommercial
4.0 International License
Activities
What's Next?
46
● Concept Chapter: 2.1 C Understanding Activities and Intents
● Practical: 2.1 P Create and Start Activities
Android Developer Fundamentals
END
47

More Related Content

Similar to 2.1 Activities and Intents.pdf

6-Fragments_081836.pptx
6-Fragments_081836.pptx6-Fragments_081836.pptx
6-Fragments_081836.pptxab2478037
 
Android introduction
Android introductionAndroid introduction
Android introductionDurai S
 
Delegating user tasks in applications
Delegating user tasks in applicationsDelegating user tasks in applications
Delegating user tasks in applicationsFriedger Müffke
 
1.2 Views, Layouts, and Resources.pptx.pdf
1.2 Views, Layouts, and Resources.pptx.pdf1.2 Views, Layouts, and Resources.pptx.pdf
1.2 Views, Layouts, and Resources.pptx.pdfSantoshKumar326148
 
Android Jump Start
Android Jump StartAndroid Jump Start
Android Jump StartHaim Michael
 
Android Development: Build Android App from Scratch
Android Development: Build Android App from ScratchAndroid Development: Build Android App from Scratch
Android Development: Build Android App from ScratchTaufan Erfiyanto
 
Android Study Jam - Introduction
Android Study Jam - IntroductionAndroid Study Jam - Introduction
Android Study Jam - IntroductionHitanshDoshi
 
Basics of Android
Basics of Android Basics of Android
Basics of Android sabi_123
 
How to create android applications
How to create android applicationsHow to create android applications
How to create android applicationsTOPS Technologies
 
June 2014 - Android wear
June 2014 - Android wearJune 2014 - Android wear
June 2014 - Android wearBlrDroid
 
Mobile application development
Mobile application developmentMobile application development
Mobile application developmentumesh patil
 
Android application development workshop day1
Android application development workshop   day1Android application development workshop   day1
Android application development workshop day1Borhan Otour
 
Lecture #1 Creating your first android project
Lecture #1  Creating your first android projectLecture #1  Creating your first android project
Lecture #1 Creating your first android projectVitali Pekelis
 

Similar to 2.1 Activities and Intents.pdf (20)

6-Fragments_081836.pptx
6-Fragments_081836.pptx6-Fragments_081836.pptx
6-Fragments_081836.pptx
 
Notes Unit3.pptx
Notes Unit3.pptxNotes Unit3.pptx
Notes Unit3.pptx
 
Android introduction
Android introductionAndroid introduction
Android introduction
 
Delegating user tasks in applications
Delegating user tasks in applicationsDelegating user tasks in applications
Delegating user tasks in applications
 
Android session 2
Android session 2Android session 2
Android session 2
 
Android Study Jams - Session 1
Android Study Jams - Session 1Android Study Jams - Session 1
Android Study Jams - Session 1
 
1.2 Views, Layouts, and Resources.pptx.pdf
1.2 Views, Layouts, and Resources.pptx.pdf1.2 Views, Layouts, and Resources.pptx.pdf
1.2 Views, Layouts, and Resources.pptx.pdf
 
Android Jump Start
Android Jump StartAndroid Jump Start
Android Jump Start
 
Android Development: Build Android App from Scratch
Android Development: Build Android App from ScratchAndroid Development: Build Android App from Scratch
Android Development: Build Android App from Scratch
 
Android Study Jam - Introduction
Android Study Jam - IntroductionAndroid Study Jam - Introduction
Android Study Jam - Introduction
 
Basics of Android
Basics of Android Basics of Android
Basics of Android
 
How to create android applications
How to create android applicationsHow to create android applications
How to create android applications
 
June 2014 - Android wear
June 2014 - Android wearJune 2014 - Android wear
June 2014 - Android wear
 
Mobile application development
Mobile application developmentMobile application development
Mobile application development
 
Internship presentation
Internship presentationInternship presentation
Internship presentation
 
What's in an Android?
What's in an Android?What's in an Android?
What's in an Android?
 
Android application development workshop day1
Android application development workshop   day1Android application development workshop   day1
Android application development workshop day1
 
Android class provider in mumbai
Android class provider in mumbaiAndroid class provider in mumbai
Android class provider in mumbai
 
Lecture #1 Creating your first android project
Lecture #1  Creating your first android projectLecture #1  Creating your first android project
Lecture #1 Creating your first android project
 
Pertemuan 3 pm
Pertemuan 3   pmPertemuan 3   pm
Pertemuan 3 pm
 

Recently uploaded

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 

Recently uploaded (20)

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 

2.1 Activities and Intents.pdf

  • 1. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities 1 1 Activities and Intents 1 Android Developer Fundamentals Lesson 2
  • 2. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities 2.1 Activities 2 This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
  • 3. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Contents 3 ● Activities ● Defining an activity ● Starting a new activity with an intent ● Passing data between activities with extras ● Navigating between activities
  • 5. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities What is an Activity? 5 ● An Activity is an application component ● Represents one window, one hierarchy of views ● Typically fills the screen, but can be embedded in other activity or a appear as floating window ● Java class, typically one activity in one file
  • 6. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities What does an Activity do? 6 ● Represents an activity, such as ordering groceries, sending email, or getting directions ● Handles user interactions, such as button clicks, text entry, or login verification ● Can start other activities in the same or other apps ● Has a life cycle—is created, started, runs, is paused, resumed, stopped, and destroyed
  • 7. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Examples of activities 7
  • 8. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Apps and activities 8 ● Activities are loosely tied together to make up an app ● First activity user sees is typically called "main activity" ● Activities can be organized in parent-child relationships in the Android manifest to aid navigation
  • 9. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Layouts and Activities ● An activity typically has a UI layout ● Layout is usually defined in one or more XML files ● Activity "inflates" layout as part of being created 9
  • 11. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Implement new activities 11 1. Define layout in XML 2. Define Activity Java class ○ extends AppCompatActivity 3. Connect Activity with Layout ○ Set content view in onCreate() 4. Declare Activity in the Android manifest
  • 12. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities 1.Define layout in XML <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Let's Shop for Food!" /> </RelativeLayout> 12
  • 13. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities 2. Define Activity Java class public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } } 13
  • 14. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities 3. Connect activity with layout public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } 14 in this XML file is layout Resource
  • 15. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities 4. Declare activity in Android manifest <activity android:name=".MainActivity"> 15
  • 16. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities 4. Declare main activity in manifest Main Activity needs to include intent to start from launcher icon <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 16
  • 18. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities What is an intent? 18 An intent is a description of an operation to be performed. An Intent is an object used to request an action from another app component via the Android system. App component Originator Intent Action Android System
  • 19. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities What can intents do? 19 ● Start activities ○ A button click starts a new activity for text entry ○ Clicking Share opens an app that allows you to post a photo ● Start services ○ Initiate downloading a file in the background ● Deliver broadcasts ○ The system informs everybody that the phone is now charging
  • 20. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Explicit and implicit intents 20 Explicit Intent ● Starts a specific activity ○ Request tea with milk delivered by Nikita ○ Main activity starts the ViewShoppingCart activity Implicit Intent ● Asks system to find an activity that can handle this request ○ Find an open store that sells green tea ○ Clicking Share opens a chooser with a list of apps
  • 22. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Start an Activity with an explicit intent To start a specific activity, use an explicit intent 1. Create an intent ○ Intent intent = new Intent(this, ActivityName.class); 2. Use the intent to start the activity ○ startActivity(intent); 22
  • 23. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Start an Activity with implicit intent To ask Android to find an Activity to handle your request, use an implicit intent 1. Create an intent ○ Intent intent = new Intent(action, uri); 2. Use the intent to start the activity ○ startActivity(intent); 23
  • 24. Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Implicit Intents - Examples 24 Show a web page Uri uri = Uri.parse("http://www.google.com"); Intent it = new Intent(Intent.ACTION_VIEW,uri); startActivity(it); Dial a phone number Uri uri = Uri.parse("tel:8005551234"); Intent it = new Intent(Intent.ACTION_DIAL, uri); startActivity(it);
  • 25. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities How Activities Run ● All activities are managed by the Android runtime ● Started by an "intent", a message to the Android runtime to run an activity 25 MainActivity What do you want to do? FoodListActivity Choose food items...Next OrderActivity Place order User clicks launcher icon Android System Intent: Start app Start main activity Android System Start choose food activity Android System Start finish order activity Intent: Shop Intent: order
  • 26. Android Developer Fundamentals Sending and Receiving Data 26
  • 27. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Two types of sending data with intents ● Data—one piece of information whose data location can be represented by an URI ● Extras—one or more pieces of information as a collection of key-value pairs in a Bundle 27
  • 28. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Sending and retrieving data In the first (sending) activity: 1. Create the Intent object 2. Put data or extras into that intent 3. Start the new activity with startActivity() In the second (receiving) activity,: 1. Get the intent object the activity was started with 2. Retrieve the data or extras from the Intent object 28
  • 29. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Putting a URI as intent data // A web page URL intent.setData( Uri.parse("http://www.google.com")); // a Sample file URI intent.setData( Uri.fromFile(new File("/sdcard/sample.jpg"))); 29
  • 30. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Put information into intent extras ● putExtra(String name, int value) ⇒ intent.putExtra("level", 406); ● putExtra(String name, String[] value) ⇒ String[] foodList = {"Rice", "Beans", "Fruit"}; intent.putExtra("food", foodList); ● putExtras(bundle); ⇒ if lots of data, first create a bundle and pass the bundle. ● See documentation for all 30
  • 31. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Sending data to an activity with extras public static final String EXTRA_MESSAGE_KEY = "com.example.android.twoactivities.extra.MESSAGE"; Intent intent = new Intent(this, SecondActivity.class); String message = "Hello Activity!"; intent.putExtra(EXTRA_MESSAGE_KEY, message); startActivity(intent); 31
  • 32. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Get data from intents ● getData(); ⇒ Uri locationUri = intent.getData(); ● int getIntExtra (String name, int defaultValue) ⇒ int level = intent.getIntExtra("level", 0); ● Bundle bundle = intent.getExtras(); ⇒ Get all the data at once as a bundle. ● See documentation for all 32
  • 33. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Returning data to the starting activity 1. Use startActivityForResult() to start the second activity 2. To return data from the second Activity: ● Create a new Intent ● Put the response data in the Intent using putExtra() ● Set the result to Activity.RESULT_OK or RESULT_CANCELED, if the user cancelled out ● call finish() to close the activity 1. Implement onActivityResult() in first activity 33
  • 34. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities startActivityForResult() startActivityForResult(intent, requestCode); ● Starts activity (intent), assigns it identifier (requestCode) ● Returns data via intent extras ● When done, pop stack, return to previous activity, and execute onActivityResult() callback to process returned data ● Use requestCode to identify which activity has "returned" 34
  • 35. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities 1.startActivityForResult() Example public static final int CHOOSE_FOOD_REQUEST = 1; Intent intent = new Intent(this, ChooseFoodItemsActivity.class); startActivityForResult(intent, CHOOSE_FOOD_REQUEST); 35
  • 36. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities 2.Return data and finish second activity // Create an intent Intent replyIntent = new Intent(); // Put the data to return into the extra replyIntent.putExtra(EXTRA_REPLY, reply); // Set the activity's result to RESULT_OK setResult(RESULT_OK, replyIntent); // Finish the current activity finish(); 36
  • 37. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities 3.Implement onActivityResult() public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TEXT_REQUEST) { // Identify activity if (resultCode == RESULT_OK) { // Activity succeeded String reply = data.getStringExtra(SecondActivity.EXTRA_REPLY); // … do something with the data }}} 37
  • 39. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Activity stack ● When a new activity is started, the previous activity is stopped and pushed on the activity back stack ● Last-in-first-out-stack—when the current activity ends, or the user presses the Back button, it is popped from the stack and the previous activity resumes 39
  • 40. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Activity Stack 40 MainActivity What do you want to do? FoodListActivity Choose food items CartActivity View shopping cart MainActivity What do you want to do? FoodListActivity Choose food items MainActivity What do you want to do? FoodListActivity Choose food items CartActivity View shopping cart OrderActivity Place order MainActivity What do you want to do? After viewing shopping cart, user decides to add more items, then places order.
  • 41. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Two forms of navigation Temporal or back navigation ● provided by the device's back button ● controlled by the Android system's back stack Ancestral or up navigation ● provided by the app's action bar ● controlled by defining parent-child relationships between activities in the Android manifest 41
  • 42. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Back navigation ● Back stack preserves history of recently viewed screens ● Back stack contains all the activities that have been launched by the user in reverse order for the current task ● Each task has its own back stack ● Switching between tasks activates that task's back stack ● Launching an activity from the home screen starts a new task ● Navigate between tasks with the overview or recent tasks screen 42
  • 43. Android Developer Fundamentals Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Up navigation 43 ● Goes to parent of current activity ● Define an activity's parent in Android manifest ● Set parentActivityName <activity android:name=".ShowDinnerActivity" android:parentActivityName=".MainActivity" > </activity>
  • 45. Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities Learn more ● Android Application Fundamentals ● Starting Another Activity ● Activity (API Guide) ● Activity (API Reference) ● Intents and Intent Filters (API Guide) ● Intent (API Reference) ● Navigation 45
  • 46. Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Activities What's Next? 46 ● Concept Chapter: 2.1 C Understanding Activities and Intents ● Practical: 2.1 P Create and Start Activities