In the bustling realm of Android development, Activities and Intents stand as key players, orchestrating the dynamic ballet of user experiences.
Activities:
Picture a stage, a singular focus, where the user engages with your application. This is the domain of Activities, the choreographers of the Android interface. An Activity encapsulates a singular, focused task that a user can do. Be it launching an app, composing an email, or viewing an image, each activity unfolds as a distinct scene in the user's journey.
Think of an Activity as a window to your application's soul, a canvas where the graphical elements and interactive fragments harmonize. It manages the UI and user interactions, breathing life into the app. The lifecycle of an Activity, akin to a theatrical performance, ensures a graceful entrance, a captivating act, and a poised exit.
Intents:
Now, imagine a messenger, carrying tidings between these Activities, enabling seamless transitions and fostering communication. This messenger is an Intent. An Intent is like a letter, encapsulating an intention or a request for an action to be performed. It acts as a bridge, connecting different components within your application or even between different applications.
Intents come in two flavors: explicit and implicit. An explicit Intent explicitly names the target component, while an implicit Intent declares the desired action without specifying a particular target. It's the choreography of Intents that allows Activities to collaborate, passing data and invoking functionalities with a fluid elegance.
In this symphony of Android development, Activities take the center stage, showcasing the user interface, while Intents, like skilled conductors, harmonize the transitions and interactions, creating a seamless and immersive experience. Together, they compose the vibrant tapestry of Android applications, where every tap and swipe unveils a new act in the digital ballet.
Implicit Intents: A Brief Overview
Implicit intents are a fundamental concept in Android application development, facilitating communication between different components of an app or even between different apps. Unlike explicit intents, which explicitly specify the target component, implicit intents do not specify a particular target, allowing the Android system to determine the most suitable component to fulfill the request.
Here are key aspects to understand about implicit intents:
1. Action and Data:
Implicit intents consist of two essential components - action and data. The action represents the desired operation, such as viewing, editing, or sending data. The data, often expressed as a URI, provides specifics about the content to be acted upon.
2. Dynamic Component Resolution:
When an implicit intent is launched, the Android system evaluates the available components that can handle the specified action and data. This dynamic resolution allows for greater flexibility, as new components can be added without modifying the original code.
3. Flexibility and Reusability:
Implicit intents enhance code flexibility and reusability. Developers can create components that respond to common actions and data types, making it easier to integrate with other apps. For instance, an image viewer component could handle implicit intents for viewing images, irrespective of the source app.
4. Example Scenarios:
Opening a Web Page: An app can use an implicit intent to request the system to open a URL, and the system will launch the user's preferred web browser.
Sharing Content: Implicit intents are often employed for sharing content, allowing users to choose from a list of compatible apps for sharing text, images, or other data.
5. Intent Filters:
Components that can respond to implicit intents declare their capabilities through intent filters in the app's manifest file. These filters specify the supported actions, data types, and URI schemes. Intent filters help the system identify suitable components for a given implicit intent.
6. Security Considerations:
While implicit intents offer flexibility, developers should be mindful of potential security risks. The system relies on intent filters, and if not properly defined, it might lead to unintended components handling sensitive data.
In conclusion, implicit intents play a crucial role in fostering interoperability and communication within the Android ecosystem. Their dynamic nature and adaptability make them a powerful tool for building versatile and interconnected Android applications. Understanding how to harness the capabilities of implicit intents is essential for developers aiming to create seamless and user-friendly Android experiences.
1. Understanding Intents
1.1 Notes
1.2 Available Intents in Android
1.3 Using Extra Information
1.4 Using Extra Information (cont.)
1.5 Using Components to Directly Invoke an Activity
1.6 Using Components to Directly Invoke an Activity (cont.)
2. Activity
2.1 Activity Lifecycle
2.2 Launching Activities and Sub-Activities(cont.)
3. Exercise 6
Android Development: Build Android App from ScratchTaufan Erfiyanto
a month ago, i have a change to held a one day workshop about Android development at Phetchaburi Rajabhat University, Thailand. The workshop is attended by students and lecturers. it is a pleasure to me to see many people that excited to learn android development.
This is a presentation slide of my presentation. in this presentation you will learn about how to build create a simple to do list app from scratch.
content of the presentation slide:
- Introduction: what Android is?
- Android development tools
- Creating Android Project
- Understanding the Project Structure
- Creating Android Emulator
- Running Android Project on Emulator
- Running Android Project on Device
- Creating User Interface
- Manipulating Widget
- Creating To Do List App
- Integrating SQLite Database to Android Project
Implicit Intents: A Brief Overview
Implicit intents are a fundamental concept in Android application development, facilitating communication between different components of an app or even between different apps. Unlike explicit intents, which explicitly specify the target component, implicit intents do not specify a particular target, allowing the Android system to determine the most suitable component to fulfill the request.
Here are key aspects to understand about implicit intents:
1. Action and Data:
Implicit intents consist of two essential components - action and data. The action represents the desired operation, such as viewing, editing, or sending data. The data, often expressed as a URI, provides specifics about the content to be acted upon.
2. Dynamic Component Resolution:
When an implicit intent is launched, the Android system evaluates the available components that can handle the specified action and data. This dynamic resolution allows for greater flexibility, as new components can be added without modifying the original code.
3. Flexibility and Reusability:
Implicit intents enhance code flexibility and reusability. Developers can create components that respond to common actions and data types, making it easier to integrate with other apps. For instance, an image viewer component could handle implicit intents for viewing images, irrespective of the source app.
4. Example Scenarios:
Opening a Web Page: An app can use an implicit intent to request the system to open a URL, and the system will launch the user's preferred web browser.
Sharing Content: Implicit intents are often employed for sharing content, allowing users to choose from a list of compatible apps for sharing text, images, or other data.
5. Intent Filters:
Components that can respond to implicit intents declare their capabilities through intent filters in the app's manifest file. These filters specify the supported actions, data types, and URI schemes. Intent filters help the system identify suitable components for a given implicit intent.
6. Security Considerations:
While implicit intents offer flexibility, developers should be mindful of potential security risks. The system relies on intent filters, and if not properly defined, it might lead to unintended components handling sensitive data.
In conclusion, implicit intents play a crucial role in fostering interoperability and communication within the Android ecosystem. Their dynamic nature and adaptability make them a powerful tool for building versatile and interconnected Android applications. Understanding how to harness the capabilities of implicit intents is essential for developers aiming to create seamless and user-friendly Android experiences.
1. Understanding Intents
1.1 Notes
1.2 Available Intents in Android
1.3 Using Extra Information
1.4 Using Extra Information (cont.)
1.5 Using Components to Directly Invoke an Activity
1.6 Using Components to Directly Invoke an Activity (cont.)
2. Activity
2.1 Activity Lifecycle
2.2 Launching Activities and Sub-Activities(cont.)
3. Exercise 6
Android Development: Build Android App from ScratchTaufan Erfiyanto
a month ago, i have a change to held a one day workshop about Android development at Phetchaburi Rajabhat University, Thailand. The workshop is attended by students and lecturers. it is a pleasure to me to see many people that excited to learn android development.
This is a presentation slide of my presentation. in this presentation you will learn about how to build create a simple to do list app from scratch.
content of the presentation slide:
- Introduction: what Android is?
- Android development tools
- Creating Android Project
- Understanding the Project Structure
- Creating Android Emulator
- Running Android Project on Emulator
- Running Android Project on Device
- Creating User Interface
- Manipulating Widget
- Creating To Do List App
- Integrating SQLite Database to Android Project
Certainly! In Android development, a "fragment" is a modular and reusable component that represents a portion of a user interface or behavior within an activity. Fragments were introduced to support the creation of flexible and responsive user interfaces, especially for larger screens like tablets.
Here are key points about fragments in Android development:
Modularity: Fragments allow developers to break down the user interface and behavior into smaller, independent components. Each fragment can have its own layout, lifecycle, and functionality.
Reusability: Fragments promote code reuse by encapsulating specific pieces of functionality. This can make it easier to manage and maintain code, especially when building applications that run on different screen sizes and orientations.
Lifecycle: Fragments have their own lifecycle, similar to activities. This includes methods such as onCreate(), onStart(), onResume(), etc. This allows developers to manage the state and behavior of each fragment independently.
Dynamic UI: Fragments are often used in conjunction with activities to create dynamic user interfaces. They can be added, removed, or replaced at runtime, allowing for more flexible and adaptive layouts.
Communication between Fragments: Fragments can communicate with each other through the associated activity. This is typically done using interfaces or direct method calls. This enables coordination between different parts of the user interface.
Back Stack: Fragments can be added to a back stack, allowing users to navigate through the fragments similarly to how they navigate through activities. This is particularly useful for maintaining a consistent user experience.
Support for Tablets and Larger Screens: Fragments are especially useful in building applications for larger screens, where multiple fragments can be combined to create a rich and multi-pane user interface. This is commonly seen in tablet applications.
Fragment Manager: The Fragment Manager is responsible for managing the lifecycle and navigation of fragments. It allows for dynamic addition, removal, and replacement of fragments within an activity.
Fragment Transactions: Changes to fragments are typically performed through fragment transactions. These transactions allow you to add, replace, or remove fragments, and they can be committed to apply the changes.
Understanding and effectively using fragments is crucial for Android developers, especially when building applications that need to adapt to various screen sizes and form factors. Fragments provide a powerful mechanism for creating flexible and modular user interfaces in Android applications.
Introduction to the basic aspects of application development in Android. The basic components, e.g., Activity and View, are introduced along with the different configuration files, e.g., AndroidManifest.xml.
Android Classes In Mumbai
best android classes in mumbai with job assistance.
our features are:
expert guidance by it industry professionals
lowest fees of 5000
practical exposure to handle projects
well equiped lab
after course resume writing guidance
OESF Authorized Training Course official textbook
Course: Android Application Development Advanced
FULL VERSION
Language: English
Contributed by
Created by: TOPGATE Inc.
Edited by: ISB Vietnam Co.,Ltd.
[Attention!]
This textbook is licensed under the Creative Commons License BY-NC-SA 4.0. It is prohibited to use this material for commercial use otherwise you are OESF member or OESF education consortium member.
TOPS Technologies offer Professional Android Training in Ahmedabad.
Ahmedabad Office (C G Road)
903 Samedh Complex,
Next to Associated Petrol Pump,
CG Road,
Ahmedabad 380009.
http://www.tops-int.com/live-project-training-android.html
Most experienced IT Training Institute in Ahmedabad known for providing Android course as per
Industry Standards and Requirement.
The slides introduce the concept of delegating user tasks in applications through messaging and app resolution of the application platform. The concept was implemented in the first version of the Android platform as is now becoming popular on other platforms as well. The W3C is discussing a draft for web intents, Mozilla proposed a document about web activities and windows provides contracts for certain user tasks.
The most popular task is "sharing". The user has a huge selection of available services from social medias, to email, from printing services to online storage. No application can ever implement all possibilities. Therefore, the application framework provides a selection of the user's preferred services. The different approaches are compared and code examples are given.
Certainly! In Android development, a "fragment" is a modular and reusable component that represents a portion of a user interface or behavior within an activity. Fragments were introduced to support the creation of flexible and responsive user interfaces, especially for larger screens like tablets.
Here are key points about fragments in Android development:
Modularity: Fragments allow developers to break down the user interface and behavior into smaller, independent components. Each fragment can have its own layout, lifecycle, and functionality.
Reusability: Fragments promote code reuse by encapsulating specific pieces of functionality. This can make it easier to manage and maintain code, especially when building applications that run on different screen sizes and orientations.
Lifecycle: Fragments have their own lifecycle, similar to activities. This includes methods such as onCreate(), onStart(), onResume(), etc. This allows developers to manage the state and behavior of each fragment independently.
Dynamic UI: Fragments are often used in conjunction with activities to create dynamic user interfaces. They can be added, removed, or replaced at runtime, allowing for more flexible and adaptive layouts.
Communication between Fragments: Fragments can communicate with each other through the associated activity. This is typically done using interfaces or direct method calls. This enables coordination between different parts of the user interface.
Back Stack: Fragments can be added to a back stack, allowing users to navigate through the fragments similarly to how they navigate through activities. This is particularly useful for maintaining a consistent user experience.
Support for Tablets and Larger Screens: Fragments are especially useful in building applications for larger screens, where multiple fragments can be combined to create a rich and multi-pane user interface. This is commonly seen in tablet applications.
Fragment Manager: The Fragment Manager is responsible for managing the lifecycle and navigation of fragments. It allows for dynamic addition, removal, and replacement of fragments within an activity.
Fragment Transactions: Changes to fragments are typically performed through fragment transactions. These transactions allow you to add, replace, or remove fragments, and they can be committed to apply the changes.
Understanding and effectively using fragments is crucial for Android developers, especially when building applications that need to adapt to various screen sizes and form factors. Fragments provide a powerful mechanism for creating flexible and modular user interfaces in Android applications.
Introduction to the basic aspects of application development in Android. The basic components, e.g., Activity and View, are introduced along with the different configuration files, e.g., AndroidManifest.xml.
Android Classes In Mumbai
best android classes in mumbai with job assistance.
our features are:
expert guidance by it industry professionals
lowest fees of 5000
practical exposure to handle projects
well equiped lab
after course resume writing guidance
OESF Authorized Training Course official textbook
Course: Android Application Development Advanced
FULL VERSION
Language: English
Contributed by
Created by: TOPGATE Inc.
Edited by: ISB Vietnam Co.,Ltd.
[Attention!]
This textbook is licensed under the Creative Commons License BY-NC-SA 4.0. It is prohibited to use this material for commercial use otherwise you are OESF member or OESF education consortium member.
TOPS Technologies offer Professional Android Training in Ahmedabad.
Ahmedabad Office (C G Road)
903 Samedh Complex,
Next to Associated Petrol Pump,
CG Road,
Ahmedabad 380009.
http://www.tops-int.com/live-project-training-android.html
Most experienced IT Training Institute in Ahmedabad known for providing Android course as per
Industry Standards and Requirement.
The slides introduce the concept of delegating user tasks in applications through messaging and app resolution of the application platform. The concept was implemented in the first version of the Android platform as is now becoming popular on other platforms as well. The W3C is discussing a draft for web intents, Mozilla proposed a document about web activities and windows provides contracts for certain user tasks.
The most popular task is "sharing". The user has a huge selection of available services from social medias, to email, from printing services to online storage. No application can ever implement all possibilities. Therefore, the application framework provides a selection of the user's preferred services. The different approaches are compared and code examples are given.
Delivering Micro-Credentials in Technical and Vocational Education and TrainingAG2 Design
Explore how micro-credentials are transforming Technical and Vocational Education and Training (TVET) with this comprehensive slide deck. Discover what micro-credentials are, their importance in TVET, the advantages they offer, and the insights from industry experts. Additionally, learn about the top software applications available for creating and managing micro-credentials. This presentation also includes valuable resources and a discussion on the future of these specialised certifications.
For more detailed information on delivering micro-credentials in TVET, visit this https://tvettrainer.com/delivering-micro-credentials-in-tvet/
A review of the growth of the Israel Genealogy Research Association Database Collection for the last 12 months. Our collection is now passed the 3 million mark and still growing. See which archives have contributed the most. See the different types of records we have, and which years have had records added. You can also see what we have for the future.
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...Levi Shapiro
Letter from the Congress of the United States regarding Anti-Semitism sent June 3rd to MIT President Sally Kornbluth, MIT Corp Chair, Mark Gorenberg
Dear Dr. Kornbluth and Mr. Gorenberg,
The US House of Representatives is deeply concerned by ongoing and pervasive acts of antisemitic
harassment and intimidation at the Massachusetts Institute of Technology (MIT). Failing to act decisively to ensure a safe learning environment for all students would be a grave dereliction of your responsibilities as President of MIT and Chair of the MIT Corporation.
This Congress will not stand idly by and allow an environment hostile to Jewish students to persist. The House believes that your institution is in violation of Title VI of the Civil Rights Act, and the inability or
unwillingness to rectify this violation through action requires accountability.
Postsecondary education is a unique opportunity for students to learn and have their ideas and beliefs challenged. However, universities receiving hundreds of millions of federal funds annually have denied
students that opportunity and have been hijacked to become venues for the promotion of terrorism, antisemitic harassment and intimidation, unlawful encampments, and in some cases, assaults and riots.
The House of Representatives will not countenance the use of federal funds to indoctrinate students into hateful, antisemitic, anti-American supporters of terrorism. Investigations into campus antisemitism by the Committee on Education and the Workforce and the Committee on Ways and Means have been expanded into a Congress-wide probe across all relevant jurisdictions to address this national crisis. The undersigned Committees will conduct oversight into the use of federal funds at MIT and its learning environment under authorities granted to each Committee.
• The Committee on Education and the Workforce has been investigating your institution since December 7, 2023. The Committee has broad jurisdiction over postsecondary education, including its compliance with Title VI of the Civil Rights Act, campus safety concerns over disruptions to the learning environment, and the awarding of federal student aid under the Higher Education Act.
• The Committee on Oversight and Accountability is investigating the sources of funding and other support flowing to groups espousing pro-Hamas propaganda and engaged in antisemitic harassment and intimidation of students. The Committee on Oversight and Accountability is the principal oversight committee of the US House of Representatives and has broad authority to investigate “any matter” at “any time” under House Rule X.
• The Committee on Ways and Means has been investigating several universities since November 15, 2023, when the Committee held a hearing entitled From Ivory Towers to Dark Corners: Investigating the Nexus Between Antisemitism, Tax-Exempt Universities, and Terror Financing. The Committee followed the hearing with letters to those institutions on January 10, 202
Executive Directors Chat Leveraging AI for Diversity, Equity, and InclusionTechSoup
Let’s explore the intersection of technology and equity in the final session of our DEI series. Discover how AI tools, like ChatGPT, can be used to support and enhance your nonprofit's DEI initiatives. Participants will gain insights into practical AI applications and get tips for leveraging technology to advance their DEI goals.
The simplified electron and muon model, Oscillating Spacetime: The Foundation...RitikBhardwaj56
Discover the Simplified Electron and Muon Model: A New Wave-Based Approach to Understanding Particles delves into a groundbreaking theory that presents electrons and muons as rotating soliton waves within oscillating spacetime. Geared towards students, researchers, and science buffs, this book breaks down complex ideas into simple explanations. It covers topics such as electron waves, temporal dynamics, and the implications of this model on particle physics. With clear illustrations and easy-to-follow explanations, readers will gain a new outlook on the universe's fundamental nature.
A workshop hosted by the South African Journal of Science aimed at postgraduate students and early career researchers with little or no experience in writing and publishing journal articles.
This slide is special for master students (MIBS & MIFB) in UUM. Also useful for readers who are interested in the topic of contemporary Islamic banking.
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
2.1-Activities and Intents.pptx
1. Android Developer Fundamentals V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
Android Developer Fundamentals V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents 1
1
Activities and
Intents
1
Android Developer Fundamentals V2
Lesson 2
2. Android Developer Fundamentals V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
2.1 Activities and Intents
2
3. Android Developer Fundamentals V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
Examples of activities
7
8. Android Developer Fundamentals V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
2. Define Activity Java class
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
13
14. Android Developer Fundamentals V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
4. Declare activity in Android manifest
<activity android:name=".MainActivity">
15
16. Android Developer Fundamentals V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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>
16
18. Android Developer Fundamentals V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
What can intents do?
19
● 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
20. Android Developer Fundamentals V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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
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
27. Android Developer Fundamentals V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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
41
42. Android Developer Fundamentals V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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
42
43. Android Developer Fundamentals V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
Up navigation
43
● Goes to parent of current Activity
● Define an Activity parent in Android manifest
● Set parentActivityName
<activity
android:name=".ShowDinnerActivity"
android:parentActivityName=".MainActivity" >
</activity>
45. Android Developer Fundamentals V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
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 V2
This work is licensed under a Creative
Commons Attribution 4.0 International
License.
Activities and
Intents
What's Next?
46
● Concept Chapter: 2.1 Activities and Intents
● Practical: 2.1 Activities and intents