0% found this document useful (0 votes)
14 views34 pages

Week 3 Activities Intent

The document covers key concepts in Android mobile application development, focusing on Intents, Activities, the Android Manifest File, and the Activity Life Cycle. It explains how Intents facilitate communication between different Activities and Services, and details the process of starting Activities, returning results, and managing the application life cycle. Additionally, it discusses the importance of the Android Manifest and how to handle configuration changes and activity states effectively.

Uploaded by

sarahayeesha1
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)
14 views34 pages

Week 3 Activities Intent

The document covers key concepts in Android mobile application development, focusing on Intents, Activities, the Android Manifest File, and the Activity Life Cycle. It explains how Intents facilitate communication between different Activities and Services, and details the process of starting Activities, returning results, and managing the application life cycle. Additionally, it discusses the importance of the Android Manifest and how to handle configuration changes and activity states effectively.

Uploaded by

sarahayeesha1
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/ 34

Week 3:

Activity & Intent


CET3013: MOBILE APPLICATION DEVELOPMENT
Today’s Topics
Intents
Activities
The Android Manifest File
Activity Life Cycle
Intents
What they are:
(one screen means one activity)(if from one
Starting activities screen jump to another screen, then is another
activity already)
Implicit Intents
Returning results
Native actions
Intents  Mainly is calling something and do a
message passing (bring data to another screen)

A message-passing mechanism which can:


◦ Explicitly start an Activity or Service—not necessarily one
in your application
◦ Broadcast that something has happened
◦ Request that an Activity or Service be started to perform
a specific action with or on some data
◦ “Intent resolution”
When sharing something from Facebook or others
Intent (click button Share and it will auto prompt to
Whatsapp or others), it will need Implicit Intent
(calling something automatically)

Call something manually

Source: https://alishabindal.medium.com/intents-in-android-9331dc8f5f81
Explicitly starting an Activity
Use startActivity method, passing an appropriate Intent.
Your Activity is not notified when the Activity which has been
started completes.
If this matters then you use startActivityForResult
instead.
Example

val intent = Intent(this,


MyOtherActivity::class.java)
startActivity(intent)
Implicit Intents
Instead of specifying a specific Activity to start, you
can simply specify an action to be performed with
or on some data (tel no, email, url, text, images).
Android determines the Activity to start using
“intent resolution”. Android will show the most suitable app to
open it - Open app accrodingly
This is widely used by many of the native Android
applications.
Implicitly starting an Activity
//Open browser to view the page
val intent = Intent(Intent.ACTION_VIEW,
Uri.parse(“https://www.kdupg.edu.my”))
startActivity(intent)
Come out the pad and load the telephone but
//Open caller to dial thewait
telephone number
me to click on the call button
if (somethingWeird && itDontLookGood) {
val intent = Intent(Intent.ACTION_DIAL,
Uri.parse("tel:123-4567"))
startActivity(intent)
}

ACTION_CALL - It will call automatically


Returning results
If our Activity starts another using startActivity,
o it isn’t notified when that Activity completes.
But we might often want to know this—especially if we are
using an Activity as a “modal dialog”.
In the previous version of Android, we use
startActivityForResult instead. It takes two parameters:
o A request code (int) which is later used to identify the Activity
concerned
o The Intent (as before)
Need to close the first tab before open the
previous one – after choose colour then it will
come back to the previous screen before they
choose the colour
Returning results
When the started Activity completes, it first needs to call
setResult(int, Intent) before calling finish()
The parameters to setResult are a result code and an
Intent which represents the actual result
o Typically includes a URI to refer to some data, may also include “extras”.

The calling Activity’s onActivityResult callback is


then called—we need to override it
Must put setResult (new term) (don’t use
startActivityResult anymore) and send the
result to intent to Store the data
Returning results
 However, in the new implementation of Android,
startActivityForResult and onActivityResult methods
are deprecated.
 This means that we need to call
registerForActivityResult() and declare a callback
handler to be called when an Activity returns a
result.
Returning results
 registerForActivityResult()
 takes an ActivityResultContract and an
ActivityResultCallback
 returns an ActivityResultLauncher which you will
use to launch the other activity
Examples: Registering Activities
Examples: starting Activities
 Once the launcher is ready, it can be called
and passed the intent to be launched as
follows:

 This intent should be “I”


Examples: setting results
 In the sub Activity, we have to implement the finish()
method.
 The finish() method is triggered when an activity
exits.

key data
Intent Filters
We’ve seen that we can use Intents to specify an
Activity to be started implicitly, but how does
Android know which Activity to start?
It uses Intent Filters.
oCan register an Activity as being interested in a particular
event.
oThis already happens—for the Activity created by the
wizard…
All defined as constants in the
Intent class. So refer to them as

Native Actions
Intent.ACTION_VIEW and so
on.

Action Definition
ACTION_ANSWER Open an Activity to handle an incoming call.
ACTION_CALL Brings up a phone dialler and immediately calls the specified
number. Bad practice: prefer ACTION_DIAL.
ACTION_DELETE Start up an Activity to allow specified data to be deleted.
ACTION_DIAL Start a phone dialler, pre-populated by the specified number.
ACTION_EDIT Start up an Activity to allow specified data to be edited.
ACTION_PICK Pick an item from a specified Content Provider.
Requires startActivityForResult.
ACTION_SEARCH Perform a search. Specified using an extra with the key
SearchManager.QUERY.
ACTION_SENDTO Send a message to a contact. (Eg. Email, SMS)
ACTION_VIEW View data in the most appropriate manner.
The Android Manifest
Every application must have an
AndroidManifest.xml file (with precisely that name)
in its root directory.
 The manifest file provides essential information
about your app to the Android system, which the
system must have before it can run any of the app's
code.
The Android Manifest

|
|
|
| Intent - Launcher  make it can be click and launch
|
|
|
Application Life Cycle
Applications have a lot less control over their life cycles
than in most other environments.
They need to be prepared to listen for changes in the
application state and react accordingly.
o Untimely termination is a likely thing to happen.
Each application runs:
o In its own process (thread)
o Under its own username
o In a separate instance of the VM.
Application Priority
Android does whatever it takes to remain responsive.
Processes are killed—often without warning—to free
resources for higher-priority processes.
Generally high-priority processes are those interacting with
the user—usually only one.
An application’s priority is equal to that of its highest-
priority component.
If one application depends on resources provided by
another, the secondary application has at least as high a
priority.
Application Priority Like I am playing game, but the alarm is ringing
Active Process but I cant see the alarm page, it just ring only
HIGH
 Have a component interacting 1. Active Process
with the user.
 Few in number (often only
one). Can view the app 2. Visible Process
Visible Process Just a reminder 3. Started Service
 Component visible but not in Process
foreground (partially obscured).
4. Background
Invisible – cannot see the app
Process
Close all the previous app at background automatically and restart
again to clear and have more memory (the process user will not know) 5. Empty Process
LOW
Eg. When I am playing game in landscape and have a 100 score, when I
rotate the screen, it will kill the space in background and restart again,
but the score will remain. If not kill the space at background, the score
will gone
Application Priority
Started Service Process
HIGH
 Hosting a service which has been 1. Active Process
started.
 Supports processing that doesn’t
require a UI.
2. Visible Process
Background Process
 Not interacting, not a running 3. Started Service
service. Process
 Killed if necessary.
4. Background
Empty Process Process
 Hosted application has terminated.
5. Empty Process
LOW
Runtime configuration
changes
Changes to various aspects—particularly hardware—cause
Android to terminate an activity within an application and
restart it, reloading the resources.
These changes include:
o orientation: rotating the screen
o keyboardHidden: exposing or hiding a hardware keyboard
o fontScale: the user has changed their preferred font size
Sometimes more than one event happens at the same
time.
Runtime configuration
changes
If we don’t want this to happen we can tell Android that’s
the case.
To do this, we add an android:configChanges attribute to
the Activity in the manifest file.
It’s a string. We can separate multiple values if we want by
using a “|”

android.configChanges=“orientation|
keyboardHidden”
Activities
An Activity in Android represents a screen
 Like a JFrame in Swing, or a Form in .NET.

Extends android.App.Activity.
Need one for each UI screen.
Normally occupies whole screen, but can be semi-
transparent or floating.
Needs an entry in the manifest.
The UI is created using the setContentView method.
The Activity Stack

When press back key, the activity 3 will remove (top one), and back to activity 2 (using Stack concept)
The Activity Stack
New Activity Active Activity
Back button
New activity started pressed or activity
closed

When click back key (pop out), it Last Active Activity


will go back to previous activity
 Activity

Previous Activities
Activity States
Active Interacting with the user. In the
foreground.
Paused Visible but not interacting with the user i.e.
partially obscured.
Stopped No longer visible.
Inactive
While I am playing game and want to change to landscape, it will go back to onCreate (to load again the GUI in
landscape) when I want to rotate
Configuration change (change language, rotate, font size,…) will destroy everything first and run again start
from onCreate

Activity Life Cycle


 Call the app but cant see GUI – load data & set the UI

Can see the


app screen

After onStart will go


to onResume first

Eg. When minimize the app


(the music will still playing)
Eg, The music will direct
stop and app will be close
Activity Methods
Method Reason to override

onCreate(Bundle) Initialisation
onRestart() Changes that need to be made if the activity has
been visible before, now it is becoming visible
again

onStart() Changes that need to be made now the activity is


visible
onResume() Resume paused UI updates or paused threads
Best place to restore persistent data
onPause() Suspend UI updates, threads
Probably the best place to save persistent data
onStop() Might never be called.
onDestroy() Might never be called.
onSaveInstanceState(Bundle) Used to save and restore the state of controls
onRestoreInstanceState(Bundl (Usually the default implementation is fine)
e)
Activity States (again)
Starting (1)onCreate()
(2)onStart()
(3)onRestoreInstanceState()
(4)onResume() Reload the existing data
(1)onRestart() Running
(1)onSaveInstanceState()
(2)onStart()
(2)onPause() Save existing
(3)onResume()
data
onResume(
)
Stopped Paused
(1)onSaveInstanceState()
(2)onStop()  Close the heavy resource Eg. Network connection,
database, sensor, services
onDestroy(
)
Destroyed
Android Studio (Design part)
when design, Layout_height normally should be 40-50dp

write the text in this column - when design developer will know (in
design page), when run the app user will not see this already

White colour is the baseline


When both thing is not at the same line (maybe some will be higher a bit and
some will lower a bit) at this condition, use “Show base line” (pull the baseline
from one to another) can let the alignment of both become at the same level

Jetpack can direct


access, no need
findViewById anymore

Chain – make both button in a chain and can move together


Summary
Android Manifest
Application Life Cycle and Priority
Resources
Configuration Changes
Activities
◦ Activity Stack
◦ Activity States

You might also like