02.1 Activities and Intents
02.1 Activities and Intents
Activities and
Intents
Lesson 2
setContentView(R.layout.activity_main);
}
} Resource is layout in this XML file
<activity android:name=".MainActivity">
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
Intent: Start app Start main Intent: Shop Start choose Intent: order Start finish
Android activity Android food activity Android order activity
System System System
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