android activity - cs 4720 · android activity cs 4720 – mobile application development source:...
TRANSCRIPT
![Page 1: Android Activity - CS 4720 · Android Activity CS 4720 – Mobile Application Development Source: developer.android.com. CS 4720 Activity • Conceptually, an Activity is a single](https://reader034.vdocuments.net/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f830/html5/thumbnails/1.jpg)
CS4720
AndroidActivity
CS4720– MobileApplicationDevelopment
Source:developer.android.com
![Page 2: Android Activity - CS 4720 · Android Activity CS 4720 – Mobile Application Development Source: developer.android.com. CS 4720 Activity • Conceptually, an Activity is a single](https://reader034.vdocuments.net/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f830/html5/thumbnails/2.jpg)
CS4720
Activity• Conceptually,anActivityisasinglescreenofyourapplication
• Inotherwords,anAppreallyisacollectionofrelatedActivities
• ConsidereachActivitybothascreenandafeature
• AppscanactivateActivitiesinotherApps
2
![Page 3: Android Activity - CS 4720 · Android Activity CS 4720 – Mobile Application Development Source: developer.android.com. CS 4720 Activity • Conceptually, an Activity is a single](https://reader034.vdocuments.net/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f830/html5/thumbnails/3.jpg)
CS4720
TheAppLifecycle
3
![Page 4: Android Activity - CS 4720 · Android Activity CS 4720 – Mobile Application Development Source: developer.android.com. CS 4720 Activity • Conceptually, an Activity is a single](https://reader034.vdocuments.net/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f830/html5/thumbnails/4.jpg)
CS4720
TheActivity
4
![Page 5: Android Activity - CS 4720 · Android Activity CS 4720 – Mobile Application Development Source: developer.android.com. CS 4720 Activity • Conceptually, an Activity is a single](https://reader034.vdocuments.net/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f830/html5/thumbnails/5.jpg)
CS4720
TheLifecycle• AnActivitycanbe:
– Resumed– activeandintheforegroundwithuserfocus
– Paused– active,butis“coveredup”byanotheractivitythatcurrentlyhastheuserfocus
– Stopped– currentlyresidinginthebackgroundandisnotattachedtothewindowmanagerandisnotvisible;canbekilledbysystemifmemoryisneeded
5
![Page 6: Android Activity - CS 4720 · Android Activity CS 4720 – Mobile Application Development Source: developer.android.com. CS 4720 Activity • Conceptually, an Activity is a single](https://reader034.vdocuments.net/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f830/html5/thumbnails/6.jpg)
CS4720
LifecycleCallbacks
6
![Page 7: Android Activity - CS 4720 · Android Activity CS 4720 – Mobile Application Development Source: developer.android.com. CS 4720 Activity • Conceptually, an Activity is a single](https://reader034.vdocuments.net/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f830/html5/thumbnails/7.jpg)
CS4720
LifecycleCallbacks• Makesuretomakethecorrectcallsinthecorrectmethods!
• IfyoudoeverythinginonCreate(),thenproblemscouldoccurwhenyouswitch
• Onadevicerotation,onDestroy()/onCreate()iscalled
• ConsidersavingstateusingonSaveInstanceState()toretainAppstatebetweenswitching
7
![Page 8: Android Activity - CS 4720 · Android Activity CS 4720 – Mobile Application Development Source: developer.android.com. CS 4720 Activity • Conceptually, an Activity is a single](https://reader034.vdocuments.net/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f830/html5/thumbnails/8.jpg)
CS4720
onCreate()• CalledwhenAppisstartingfresh• ThisiswheresetContentView()(settingtheUI)iscalled
• ThisiswhyonCreate()iscalledonarotation–youmaybeusingadifferentlayout
8
![Page 9: Android Activity - CS 4720 · Android Activity CS 4720 – Mobile Application Development Source: developer.android.com. CS 4720 Activity • Conceptually, an Activity is a single](https://reader034.vdocuments.net/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f830/html5/thumbnails/9.jpg)
CS4720
NextSteps• onStart()- CalledrightbeforeActivityisshowntouser
• onResume()– CalledjustbeforeActivitywillstartacceptinguserinput
• onPause()– CalledwhenanotherActivitywillcallonResume()– usedtosaveremainingchanges
• onStop()– CalledwhenActivityisnolongervisible
9
![Page 10: Android Activity - CS 4720 · Android Activity CS 4720 – Mobile Application Development Source: developer.android.com. CS 4720 Activity • Conceptually, an Activity is a single](https://reader034.vdocuments.net/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f830/html5/thumbnails/10.jpg)
CS4720
UI• UIcomponentscomefromtheViewclass• AViewcontrolsasetofrectangularspaceandcanrespondtouserinteraction
• Canbethewholescreen,orjustasmallportion
• Technically,a“widget”(likeabutton)isaverysmallView
• ALayout(andit’sxml)isagroupofViewswithlocationinstructions
10
![Page 11: Android Activity - CS 4720 · Android Activity CS 4720 – Mobile Application Development Source: developer.android.com. CS 4720 Activity • Conceptually, an Activity is a single](https://reader034.vdocuments.net/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f830/html5/thumbnails/11.jpg)
CS4720
AddingtotheManifest• AllActivitiesmustbedefinedintheandroidmanifest.xml file
• IntentsthatthisActivitycanrespondtoarealsodefinedhere
11
![Page 12: Android Activity - CS 4720 · Android Activity CS 4720 – Mobile Application Development Source: developer.android.com. CS 4720 Activity • Conceptually, an Activity is a single](https://reader034.vdocuments.net/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f830/html5/thumbnails/12.jpg)
CS4720
LaunchinganewActivity• TolaunchanewActivity,createanIntentforthatActivityandsenditout
12
![Page 13: Android Activity - CS 4720 · Android Activity CS 4720 – Mobile Application Development Source: developer.android.com. CS 4720 Activity • Conceptually, an Activity is a single](https://reader034.vdocuments.net/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f830/html5/thumbnails/13.jpg)
CS4720
Fragments• ConsideraFragmentasortof“sub-Activity”• AnActivitycanbemadeupofnumerousFragments,eachwiththeirownfeatures
• Thisisusefulwhenyouwanttomove/reordercomponentsfordifferentdisplaysizes/rotations
• AFragmentobeystheActivity’slifecyclestate
13
![Page 14: Android Activity - CS 4720 · Android Activity CS 4720 – Mobile Application Development Source: developer.android.com. CS 4720 Activity • Conceptually, an Activity is a single](https://reader034.vdocuments.net/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f830/html5/thumbnails/14.jpg)
CS4720
Fragments
14
![Page 15: Android Activity - CS 4720 · Android Activity CS 4720 – Mobile Application Development Source: developer.android.com. CS 4720 Activity • Conceptually, an Activity is a single](https://reader034.vdocuments.net/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f830/html5/thumbnails/15.jpg)
CS4720
Fragments• AFragmenthas:
– Itsownlifecyclecalls– Itsownlayouts– Itsownviews
• Infact,aFragmentlooksalmostexactlylikeanActivity!
• ToaddaFragment,instantiateitinthelayoutoftheActivity
15
![Page 16: Android Activity - CS 4720 · Android Activity CS 4720 – Mobile Application Development Source: developer.android.com. CS 4720 Activity • Conceptually, an Activity is a single](https://reader034.vdocuments.net/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f830/html5/thumbnails/16.jpg)
CS4720
FragmentManager• ThemainbenefitsFragmentsarereusabilityandaddressability
• FragmentscanbeaccessedthroughtheFragmentManager andcanbeswappedontheflywhiletheAppisrunning
• FragmentscanaddresstheActivitythatcontainsthem,allowingforsharingofdatabetweenFragments
16