Week 3 Activities Intent
Week 3 Activities Intent
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
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
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
onCreate(Bundle) Initialisation
onRestart() Changes that need to be made if the activity has
been visible before, now it is becoming visible
again
write the text in this column - when design developer will know (in
design page), when run the app user will not see this already