0% found this document useful (0 votes)
6 views18 pages

8.intents - LinkActivities FPT

fpt
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)
6 views18 pages

8.intents - LinkActivities FPT

fpt
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/ 18

Intent

& Connecting Activities


Contents
● Intent
● Starting a new Activity with an Intent
● Passing data between activities with extras
● Navigating between activities
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
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 a Service
○ Initiate downloading a file in the background
● Deliver Broadcast
○ The system informs everybody that the phone is now charging
INTENT
• Explicit Intent: Starts a specific Activity
• Implicit Intent: Asks system to find an Activity that can handle this
request
INTENT
• Explicit Intent
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(i);

• Implicit Intent
Intent read1=new Intent();
read1.setAction(android.content.Intent.ACTION_VIEW);
read1.setData(ContactsContract.Contacts.CONTENT_URI);
startActivity(read1);
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);

7
Activity Implement
● Start another Activity

- LoginActivity.this: Current activity


ProfileActivity.class: The activity need to launch
Sending and retrieving data
● In the first (sending) Activity:
• Create the Intent object
• Put data or extras into that Intent
• Start the new Activity with startActivity()
● In the second (receiving) Activity:
• Get the Intent object, the Activity was started with
• Retrieve the data or extras from the Intent object

9
Communicating Between Activities
- Pass data from Activity A to Activity B.

- Get data from Activity A at Activity B.


Communicating Between Activities
- Pass data from Activity A to Activity B and Receive a result back,
using:

- Activity A starts activity B

- Activity B sends result

- Activity A receives the result back


Communicating Between Activities
- Pass data from Activity A to Activity B and Receive a result back
(cont'd)
- Activity A receives the result back
Communicating Between Activities startAcvityForResult
is deprecated
private ActivityResultLauncher<Intent> activityResultLauncher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Register the activity result launcher
activityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
// Handle the returned data
}
}
}
);
Communicating Between Activities startAcvityForResult
is deprecated
// Example: Launch another activity
findViewById(R.id.btn_go).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
activityResultLauncher.launch(intent);
}
});
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

15
Activity Stack
vity
After viewing i
t r
Ac rde
r
shopping cart, rd
O ace
e o

user decides to rt Pl
ca
vity ing rt
add more items, cti p i v ity g ca
rtA hop rtA
c t
ppi
n
then places Ca w s OrderActivity Ca w sho
Vie Place order Vie
order.
CartActivity CartActivity A
y
ctivit ms
L i s t
View shopping cart View shopping cart Food e food ite
s
Choo
FoodListActivity FoodListActivity FoodListActivity
Choose food items Choose food items Choose food items
MainActivity MainActivity MainActivity MainActivity
What do you want to What do you want to What do you want to What do you want to
do? do? do? do?

16
Classwork
● Create LoginActivity:
○ Enter name and touch Login Button to close Login Activity & open
HomeActivity
○ Send name to HomeActivity
○ Clear field name if minimize and re-open app
● Create HomeActivity:
○ Get name from LoginActivity and show it on screen
○ Press CLOSE button to finish app
References
● https://google-developer-training.github.io/android-developer-fun
damentals-course-concepts-v2/

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