Android Activity CS 4720 – Mobile Application Development Source: developer.android.com CS 4720
Activity • Conceptually, an Activity is a single screen of your application • In other words, an App really is a collection of related Activities • Consider each Activity both a screen and a feature • Apps can activate Activities in other Apps CS 4720 2
The App Lifecycle CS 4720 3
The Activity CS 4720 4
The Lifecycle • An Activity can be: – Resumed – active and in the foreground with user focus – Paused – active, but is “covered up” by another activity that currently has the user focus – Stopped – currently residing in the background and is not attached to the window manager and is not visible; can be killed by system if memory is needed CS 4720 5
Lifecycle Callbacks CS 4720 6
Lifecycle Callbacks • Make sure to make the correct calls in the correct methods! • If you do everything in onCreate(), then problems could occur when you switch • On a device rotation, onDestroy() / onCreate() is called • Consider saving state using onSaveInstanceState() to retain App state between switching CS 4720 7
onCreate() • Called when App is starting fresh • This is where setContentView() (setting the UI) is called • This is why onCreate() is called on a rotation – you may be using a different layout CS 4720 8
Next Steps • onStart() - Called right before Activity is shown to user • onResume() – Called just before Activity will start accepting user input • onPause() – Called when another Activity will call onResume() – used to save remaining changes • onStop() – Called when Activity is no longer visible CS 4720 9
UI • UI components come from the View class • A View controls a set of rectangular space and can respond to user interaction • Can be the whole screen, or just a small portion • Technically, a “widget” (like a button) is a very small View • A Layout (and it’s xml) is a group of Views with location instructions CS 4720 10
Adding to the Manifest • All Activities must be defined in the androidmanifest.xml file • Intents that this Activity can respond to are also defined here CS 4720 11
Launching a new Activity • To launch a new Activity, create an Intent for that Activity and send it out CS 4720 12
Fragments • Consider a Fragment a sort of “sub-Activity” • An Activity can be made up of numerous Fragments, each with their own features • This is useful when you want to move/reorder components for different display sizes/rotations • A Fragment obeys the Activity’s lifecycle state CS 4720 13
Fragments CS 4720 14
Fragments • A Fragment has: – Its own lifecycle calls – Its own layouts – Its own views • In fact, a Fragment looks almost exactly like an Activity! • To add a Fragment, instantiate it in the layout of the Activity CS 4720 15
FragmentManager • The main benefits Fragments are reusability and addressability • Fragments can be accessed through the FragmentManager and can be swapped on the fly while the App is running • Fragments can address the Activity that contains them, allowing for sharing of data between Fragments CS 4720 16
Recommend
More recommend