0% found this document useful (0 votes)
157 views47 pages

02.1 Activities and Intents

This document discusses activities and intents in Android. It defines an activity as an application component that represents one window and hierarchy of views. Activities handle user interactions and have a life cycle. The document describes how to implement new activities by defining a layout, activity class, connecting the two, and declaring the activity in the manifest. It also explains that intents are objects used to request actions from app components and can be used to start activities, services, and deliver broadcasts either explicitly or implicitly.

Uploaded by

bksreejith4781
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
157 views47 pages

02.1 Activities and Intents

This document discusses activities and intents in Android. It defines an activity as an application component that represents one window and hierarchy of views. Activities handle user interactions and have a life cycle. The document describes how to implement new activities by defining a layout, activity class, connecting the two, and declaring the activity in the manifest. It also explains that intents are objects used to request actions from app components and can be used to start activities, services, and deliver broadcasts either explicitly or implicitly.

Uploaded by

bksreejith4781
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Android Developer Fundamentals V2

Activities and
Intents

Lesson 2

Activities and This work is licensed under a


Android
Android Developer
Developer Fundamentals
Fundamentals V2
V2 Intents
Creative Commons Attribution 4.0 Inter 1
national License
2.1 Activities and Intents

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 2
national License
Contents
● Activities
● Defining an Activity
● Starting a new Activity with an Intent
● Passing data between activities with extras
● Navigating between activities

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 3
national License
Activities
(high-level view)

Android Developer Fundamentals V2 4


What is an Activity?
● 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

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 5
national License
What does an Activity do?
● 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
Activities and This work is licensed under a
Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 6
national License
Examples of activities

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 7
national License
Apps and activities
● 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

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 8
national License
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

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 9
national License
Implementing
Activities

Android Developer Fundamentals V2 10


Implement new activities
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

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 11
national License
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>
Activities and This work is licensed under a
Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 12
national License
2. Define Activity Java class

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 13
national License
3. Connect activity with layout

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
}
} Resource is layout in this XML file

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 14
national License
4. Declare activity in Android manifest

<activity android:name=".MainActivity">

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 15
national License
4. Declare main activity in manifest
MainActivity needs to include intent-filter to start
from launcher
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Activities and This work is licensed under a
Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 16
national License
Intents

Android Developer Fundamentals V2 17


What is an intent?
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.

Originator App component

Intent Action
Android
System
Activities and This work is licensed under a
Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 18
national License
What can intents do?
● Start an Activity
○ A button click starts a new Activity for text entry
○ Clicking Share opens an app that allows you to post a photo

● Start an Service
○ Initiate downloading a file in the background

● Deliver Broadcast
○ The system informs everybody that the phone is now charging
Activities and This work is licensed under a
Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 19
national License
Explicit and implicit intents
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
Activities and This work is licensed under a
Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 20
national License
Starting
Activities

Android Developer Fundamentals V2 21


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);

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 22
national License
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);

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 23
national License
Implicit Intents - Examples
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);
Activities and This work is licensed under a
Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 24
national License
How Activities Run
● All Activity instances are managed by the Android runtime
● Started by an "Intent", a message to the Android runtime to
run an activity

User clicks MainActivity FoodListActivity OrderActivity


launcher icon What do you want to do? Choose food items...Next Place order

Intent: Start app Start main Intent: Shop Start choose Intent: order Start finish
Android activity Android food activity Android order activity
System System System

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 25
national License
Sending and
Receiving Data

Android Developer Fundamentals V2 26


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

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 27
national License
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:


4. Get the Intent object, the Activity was started with
5. Retrieve the data or extras from the Intent object
Activities and This work is licensed under a
Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 28
national License
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")));
Activities and This work is licensed under a
Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 29
national License
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
Activities and This work is licensed under a
Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 30
national License
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);

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 31
national License
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

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 32
national License
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
Activities and This work is licensed under a
Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 33
national License
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"
Activities and This work is licensed under a
Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 34
national License
1. startActivityForResult() Example

public static final int CHOOSE_FOOD_REQUEST = 1;

Intent intent = new Intent(this, ChooseFoodItemsActivity.class);


startActivityForResult(intent, CHOOSE_FOOD_REQUEST);

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 35
national License
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();
Activities and This work is licensed under a
Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 36
national License
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
}}}

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 37
national License
Navigation

Android Developer Fundamentals V2 38


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

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 39
national License
Activity Stack
ty
After viewing shopping tivi r
c
cart, user decides to e rA rde
rd o
add more items, then O lace
P
places order. c art
ity rt
ctiv ping t ivity ing ca
rtA op tA c p
Ca w sh OrderActivity Car shop
Vie Place order Vie
w
ity
CartActivity CartActivity d L is tActiv ms
View shopping cart View shopping cart Foo
s e f o o d it e
Choo
FoodListActivity FoodListActivity FoodListActivity
Choose food items Choose food items Choose food items
MainActivity MainActivity MainActivity MainActivity
What do you want to do? What do you want to do? What do you want to do? What do you want to do?

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 40
national License
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 Up button in app's action bar
● controlled by defining parent-child relationships between
activities in the Android manifest
Activities and This work is licensed under a
Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 41
national License
Back navigation
● Back stack preserves history of recently viewed screens
● Back stack contains all the Activity instances 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

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 42
national License
Up navigation
● Goes to parent of current Activity
● Define an Activity parent in Android manifest
● Set parentActivityName
<activity
android:name=".ShowDinnerActivity"
android:parentActivityName=".MainActivity" >
</activity>

Activities and This work is licensed under a


Android Developer Fundamentals V2 Intents
Creative Commons Attribution 4.0 Inter 43
national License
Learn more

Android Developer Fundamentals V2 44


Learn more
● Android Application Fundamentals
● Starting Another Activity
● Activity (API Guide)
● Activity (API Reference)
● Intents and Intent Filters (API Guide)
● Intent (API Reference)
● Navigation
This work is licensed under a
Activities and
Android Developer Fundamentals V2 Creative Commons Attribution 4.0 Inter 45
Intents national License
What's Next?

● Concept Chapter: 2.1 Activities and Intents


● Practical: 2.1 Activities and intents

This work is licensed under a


Activities and
Android Developer Fundamentals V2 Creative Commons Attribution 4.0 Inter 46
Intents national License
END

Android Developer Fundamentals V2 47

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy