03 Activities and Intents Flashcards
Activity
● A single screen with a user interface ● Has a unique purpose ● Strung together to accomplish some task ● Can be from other applications ● Extended from the Activity class
Manifest
● AndroidManifest.xml – each app has one ● Ties everything together
● Tells the system what components you have
● Activities, Services, ContentProviders, and BroadcastReceivers
● Registers Intents that you want to handle from other apps
● What permissions and device hardware you require
● Minimum and target platform API
Explicit Intents
● Directly tell a specific Activity or Service to do something
● The email app’s Inbox Activity tells the
MessageViewerActivity to display an email when the user clicks on a message
Implicit Intents
Real world example
(Implied)
Call on an unknown component to do something
View a photo, Send a message to someone
Ask the Android Framework: Is there an app on this device that can handle this task?
User often gets to pick what app to use, if more than one app can handle the request
Implicit Intents
● Lets anonymous application components service action requests
● Thus you can ask the system to start an Activity to
perform an action without knowing which application or activity will be started
● Cool, but be careful
● You don’t always know if your user has something on their phone to handle the intent
Statefulness
Mobile apps run on limited devices
● Memory
● CPU
● Network conditions
Mobile apps must play nicely with other apps
● Incoming calls.
● User switches to other app
● Another app might need the camera
● Low memory conditions
As devices get more advanced, this problem becomes less of a problem to “end programmers”
● But this will still be a few years away
● Will be handled by the framework more seamlessly
Will never stop being important to user experience
● Never lose your user’s data
Activity Lifecycle (Why?)
Your app should always be able to quickly go away, and then quickly come back
● But make it look like nothing happened
Why?
● Statefulness: Limited resources on mobile. Other apps want to run, too
● Screen changes. Automatically switch resources from landscape to portrait
An activity has essentially four states:
- If an activity is in the foreground of the screen (at the top of the stack), it is active or running.
- If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (maintains state and member), but can be killed by the system in extreme low memory situations.
- If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user.
- If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.
Activity lifecycle steps
● onCreate ● onStart / onRestart ● onResume ● onPause ● onSaveInstanceState ● onStop ● onDestroy
Can skip back to state if not being destroyed completely
a. OnCreate
Do most of the initialization Set the layout for the activity Find UI Views and hook up listeners Restore previous saved instance values Read Intents
As quickly as possible (under 2 seconds)
b. onSaveInstanceState
Save the state of the user
interface
The framework will do much
of this for you
c. onCreate
Restore your saved UI state
with savedInstanceState
bundle
d. onPause
Stop animations
Stop long-running operations
Save your user data to storage
Release sensors
e. onResume
Start or restart your animations
Resume long-running operations
Re-acquire sensors
Activity and Fragment Lifecycles
That is a simplified version But it pretty much covers over 95% of use cases
- onCreate/onCreateView – Create stuff. Read Intents. Restore UI
- onResume – Resume stuff you’ve paused 3. onPause – Pause stuff. Save Data.
- onSaveInstanceState… – Save UI stuff.