0% found this document useful (0 votes)
58 views30 pages

Pert 5 - Activity Intents

An activity represents a single screen in an Android app. Activities are started using explicit or implicit intents. Explicit intents directly start a specific activity class, while implicit intents allow the system to choose an activity that can handle the intent. Data can be passed between activities using extras bundled in the intent. Activities are added to a back stack, allowing the user to navigate backward through the app's history using the back button. Parent-child relationships between activities enable up navigation in the action bar.

Uploaded by

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

Pert 5 - Activity Intents

An activity represents a single screen in an Android app. Activities are started using explicit or implicit intents. Explicit intents directly start a specific activity class, while implicit intents allow the system to choose an activity that can handle the intent. Data can be passed between activities using extras bundled in the intent. Activities are added to a back stack, allowing the user to navigate backward through the app's history using the back button. Parent-child relationships between activities enable up navigation in the action bar.

Uploaded by

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

Activity & Intents

Materi 5
Contents

● Activities
● Defining an activity
● Starting a new activity with an intent
● Passing data between activities with extras
● Navigating between activities
● Activity Lifecycle
Activity
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
● Kotlin class, typically one activity in one file
● Represent the Graphical User Interface → Activity menyajikan
sebuah user interface visual yang berfokus pada kegiatan dari user
● Sebuah aplikasi dapat terdiri dari satu atau lebih activity, tergantung
aplikasi & designernya
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
● set UI setContentView(View) in activity to show the user interface.
Like when you open mail apps (Gmail etc) you will see the list of
emails. That is showing in a list on activity or using other
components.
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
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
Implement Activity
Implement new activities

1. Define layout in XML


2. Define Activity Java class
o extends AppCompatActivity
3. Connect Activity with Layout
o Set content view in onCreate()
4. Declare Activity in the Android manifest
Activity in Kotlin Class

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
print("onCreate")
}
}
Activity in Android 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>
Activity Relation in Android Project

• You can see the code of


MainActivity have set view
“activity_main.xml” and
MainActivity has declared in
AndroidManifest.xml.
Intents
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.
Intent can do

● 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
Explisit and Implisit 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
Explisit Intent – Starting an Activity

• You can start a new instance of an • Activity A creates an Intent with


Activity by passing an Intent to an action description and passes
startActivity(). The Intent describes it to startActivity().
the activity to start and carries any • The Android System searches all
necessary data. application for an intent filter
and that matches the intent.
When a match is found,
• the system starts the matching
activity (Activity B) by invoking
its onCreate() method and
passing it the Intent.
Explisit Intent - Example

buttonExplisit.setOnClickListener(View.OnClickListener
{
val intent = Intent(this@MainActivity,
SecondActivity::class.java)
startActivity(intent)
}
Implicit Intents - Examples

• Open a web page


Android Intent Structure

• For Building an Intent there is primary thing have to mention in


<intent-filter> or use flag :
• Action: A string that specifies the generic action to perform, such as
#ACTION_VIEW, #ACTION_EDIT, #ACTION_MAIN, etc.
• Category: Gives additional information about the action to execute.
For example, #CATEGORY_LAUNCHER means it should appear in the
Launcher as a top-level application.
• Data: The data to operate on, such as a personal record in the
contacts database, expressed as a android.net.Uri.
Send & Retrieve Data
Using 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
Send 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
Sample Code – Sending Intent
Sample Code – Receive 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
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
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
Up navigation

● Goes to parent of current activity


● Define an activity's parent in Android manifest
● Set parentActivityName

<activity
android:name=".ShowDinnerActivity"
android:parentActivityName=".MainActivity" >
</activity>

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