MAD Unit 2
MAD Unit 2
a n d F r a g m e n t s
rJntents
1 Activity Lifecycle
id
2 Fragments in A nd ro
en t
3 Life Cycle o f F ra gm
t
4 Replacing F ra gm en
5 Intent
r A ct iv ity
6 Intent to S ta rt an ot he
In te nt
7 lmplicir an d E xp lic it
ic e (AVD)
8 Android Virtual D ev
P ro ce ss
9 Mapping A pp lic at io n to
w er s
10 Short Questions an d A ns
11 Multiple Choice Q ue st io ns
~~~~
"!!'bile Applicatio ~~~t_ _ _ _~ _. = .: __
2 2
n [)evefopmen
2-3
/icstion Development
p;teAPP
o Paused
paLlseO f cus and enters a
of1 alls onPause() when the activity loses o B k or Recents
~~c ~s~ ~
, 'fhe ThiS state occurs when, for example, the user . . it technically means
state. When the system calls onPause() for your activ1~' . . that the user
button. t ft is an indication
. ·ty is still partially visible, but mos O en d Resumed
your acnv1 ti. ·ty and the activity will soon enter the Stoppe or
. ving the ac v1 ,
is 1ea
state. th UI if the user is
tivity in the Paused state may continue to update e h . g a
An ac . . . lude one s owm
' ting the UI to update. Examples of such an activity me . ..
expec . E .. f such activities 1ose
1
. an· on map screen or a media player playing. ven
nav1g
S th
e user expects their UI to continue updating. k
f~,
. . d ta make networ
, You should not use onPause() to save application or user a ,
calls, or execute database transactions.
, Once onPause() finishes executing, the next callback is either onStop() or
onResume(), depending on what happens after the activity enters the Paused state .
. onStop()
, The system calls onStop() when the activity is no longer visible to the user. This
may happen because the activity is being destroyed, a new activity is starting, or
an existing activity is entering a Resumed state and is covering the stopped
activity. In all of these cases, the stopped activity is no longer visible at all.
• The next callback that the system calls is either onRestart(), if the activity 18
coming back to interact with the user, or by onDestroy() if this activity 18
completely terminating.
•onD&stroy()
on Create()
onStart()
on Resume()
user navigates
to the activity
Activity
App process running
killed
onStop()
on Destroy()
Activity
shutdown
Fig. 2.1.1
fl1 Fragments in Android
• Fragment class in Android is used to build dynamic User Interfaces. Fragmenl
should be used within the Activity.
• A greatest advantage of fragments is that it simplifies the task of creating UI for
multip le screen sizes. A activity can contain any number of fragments.
• A Fragment rep resents a behavior or a portion of user interface in a
FragmentActivity.
• Multiple fragments can be combined in a single activity to build a multi-pane VI
and reuse a fragment in multiple activities.
Importance of Fragments
• There are many use cases for fragments but the most common use cases include :
1
~=ing View and Logic Components - Fragments enable re-use of parts of
Y screen mcluding views and event logic over and over m· cliff
erent ways
across many dispara te [Link]"es. For example, using the same list
dilie~ t da across
en ta sources within an app.
·~
fJjj communication between Fragments and Activity
t 1. [Link] method can return a FragmentActivity object. This
~ object is just the Fragment belongs Activity.
2. [Link]() meth
r od will retur n a
[Link] Manager instance.
I 3. [Link]() meth od can
retrie ve the desired
f Fragment object with specified id.
r 4. Call [Link]().findViewByld() meth od
to get the view controls in
that Fragment.
5. Then you can change the view control's prop erty and add even
t listener.
flJ Life Cycle of Fragment
• Each fragment has its own life cycle methods that is affec
ted by activity life cycle
because fragments are embedded in activity.
onCreatView()
onActlvltyCreated()
onStsrt()
onResume()
Fragment Is active
f onPause()
The fragment
t onStop()
returns to the
layout back
stack
r
onDestroyview()
on Destroy()
onDetach()
Fragment is destroyed
- - - - - - - - - -Fig-. -
2.3.1 Android fragment llfe cycle
----------- __ -/
TECHNICAL PUBLICATIONS® - An up
------
thrust for knowledge
/l;-l1w
~!f APr
la cing Fragment
ReP
.
[Link] g one , bu t requi,res
rocedure to. replace a fragment is similar to the
n,e P
' ceO method instead of addO.
rep 1a
men t tran sact ions is per fo~ ed, such as repl ace or remove one , it's ~fte n
' W}le n frag
rd and "undo" the change.
appropriate to allow the user to navigate backwa
· cti' ons , you
t transa
allow the use r to navigate backward through the fragmen
, To FragmmtTransaction.
must call addToBackStack{) before you commit the
add the transaction to the back stack,
, When you remove or replace a fragment and
the fragment that is removed is stopped (not
destroyed) . If the user navigates back
not add the transaction to the back
to restore the fragment, it restarts. lf you do
oved or replaced.
stack, then the fragment is destroyed when rem
:
Example of replacing one fragment with ano ther
KOTLINJAVA
clfy:1ng the article tt should show
Create fragment and give .it an argument spe
, ewFragm~nt :::! Art icle Fra gme ntO
,' :-cije args ;; Bundle()
gs:p titJn t(Ar ticle Frag men t~G_POSITION ,
position)
,:[Link]:guments = args
. ',/ ·. ·, ·, •: •:.-, ·.
[Link] ly {
:: ~~sa~ion 7 sup por tFra gme ntM ana ger .beginTran
frag men t,
:;~;~~Hl~9~ ir~,t~yer
is in the frag men t_con tain er view wit h this
back stack so the user can navigate bac k
:/2'~~ ~~§. the ttan.~action to the new
~J)la9e([Link]µ.ent Fra gm ent)
con tain er,
::;.~t··/~. ·iB~
0 t~i<P-~>1•: · . . , .>··;>ti
rrcr•·· . •.•·•· •· . : : f i:>FP
i """''''' "'" 11'/\1}\::,,\,t> ~ ',, ,,, ' ' Bv nassmg, an J;ll, • ~ft· ';:A'-"fi·11r.1·hi :.l'h nerfotm .reqmr&t thiiias·. a
;,...Startir~fai).~
,,,.,,, ac~rt;t
',.-,,.;w.,~· •., ·>·, ·. itew
~.J\< • ·ru ,,.,.
r r,:AcfiVJ.v, ·eXlS™=~" a ... , , tJ,:: :.~v, r:=, • .. . · o ·
V+-• . . . ___ · l'? ...... :.... ··--· ._....... -· .,...:. •:... -·:··......-...,-:.,.· .. ,- ...... ,,,. , w, : ,. ..,..;......,_, .......... .
i ' ' .'... '' .,, ,,..'" . . ,. . . . ...... . . . ...:•·•~ "''.. an Intemet .·ohmct ·to sendS,tC>:[Link] ~ethod we can
, INliverjpga·[Link]~~e : . By .P~ssmg · to t;ffieitagp ·[Link].c~vers. . · , ·
: '. ' : [Link].~~»~~--~~!~~~~.:...,. . . ,,..,.,..,. ..,,.,... ,. ••❖ ❖u''- ,:.:.:.,..•❖ •·❖n.<❖SW.<<«•W,WM'n❖ ,,,,,...,,,,.,,,,..,,.,.,Hu,,.,_
••,,, . ,....... , •••, w•~-'· ·· e
~
~~-H••,.,~W"'"~·--~ b.. ,iect which is used to request an action from another a~~ f,
.,,,...,..,,.."""'"'"'~---..~-----·•·--'· . •
• Intent IS a messaging o J • •
component such as activities, services, broadcast re~e1~ers and content provider!.
Your code can send them to the Android system defmmg the components you ar1
targeting. For example, via the startActivity() method you can define that ilii
intent should be used to start an activity.
• An intent can contain data via a Bundle. This data can be used by the receivini
component.
• In Android the reuse of other application components is a concept known as task
An application can access other Android components to achieve a task. For
example, from a component of your application you can trigger another
component in the Android system, which manages photos, even if this componenl
is not part of your application. In this component you select a photo and return to
your application to use the selected photo.
00~1; Val
· EXTRA
· • _MESS
. .. . .AGE. = [Link]'r»_
II •
.. ' . •-.::,;
Explicit Intent
• An explicit Intent [Link] the co mp on ent by the fully-qualified class name
es on en t in th e ow n ap p.
. the common case to sta rt
start. This is a co mp
Example :
Intent startlntent = new Inten ot he rA cti vi ty .cl a
t(myContext, An
fl1 Android Virtual Device (AVD)
r • An Android Virtual De
vice (AVD) 1s . nfi ti
characteristics of an Android a co gu ra on th at defines ~t
phone, tablet, W ea r OS, And .d TV or Automou
y OS device that you want to sim ro1 ,
ulate in the An dr oi d Em ul
ato r. .
• An Android Virtual Devic
e (AVD) is a device co nf ig th
Android emulator. It works wi ur ati on th at is run wi t,
th the em ula tor to pr ov id e
environment in which to instal a vi rtu al device-spec:
l an d ru n An dr oid ap ps .
• An AVD contains a hardw
are profile, sy ste m im ag e,
properties. sto ra ge are a, skin, and o:1'
ADT or the And roid stud io tool s con ver t these app lica tion files, tran spa ren tly
The
' to the user, into an And roid application.
in thei r IDE, the who le And roid
• When developers trigger the dep loy men t
and star ted.
application is compiled, pac kag ed, dep loy ed
nversion Process from Source Code to And
roid Application
s files by the Java compiler.
, The Java source files are con ver ted to Java clas
whi ch con ver ts Jav a class files into a
• The Android SOK contains a tool called dx
app lica tion are pla ced in this .dex
.dex (Dalvik Executable) file. All class files of the
file.
• During this conversion process red und
ant info rma tion in the class files are
e Sb'ing is fou nd in diff eren t class
optimized in the .dex [Link] example, if the sam
of this Sb'ing.
files, the .dex file contains only one reference
in size than the cor resp ond ing class
• These .dex files are therefore mu ch smaller
files.
roid project, e.g., the ima ges and XM L
• The .dex file and the resources of an And
e) file. The pro gra m aapt (An dro id
files, are packed into an .apk (Android packag
Asset Packaging Tool) performs this step.
ry data to run the And roid application
• The resulting .apk file contains all necessa
and can be deployed to an And roid device
via the adb tool.
10. GSM, EDGE and 3G networks for telephony and data transfer.
11. The Android SDK includes development tools which helps compile and debug any
app.
\ 12. Android emulator shows how app will look and behaviour on a real Android
device.