22
CHAPTER 3: Activities
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject")
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body")
startActivity(Intent.createChooser(
emailIntent, "Send email..."))
// or startActivity(emailIntent) if you want to use
// the standard chooser (or none, if there is only
// one possible receiver).
Caution
It is at the receiving app’s discretion how to exactly handle intent URIs and extra data. A
poorly designed e-mailer might not allow you to specify e-mail header data at all. To
be on the safe
side, you may want to add all header data in both the
mailto:
URI
and
as extra data.
Activities Lifecycle
Activities have a lifecycle, and contrary to traditional desktop applications, they are
intentionally subject to being killed whenever the Android OS decides to do so. So, you as
a developer need to take special precautions to make an app stable.
More precisely, an
activity finds itself in one of the following states:
Shut down
: The activity is not visible and not processing anything. Still,
the app containing the activity might be alive because it has some other
components running.
Created
: Either the activity is the main activity and was started by the
user or some other component or it is an activity regardless of whether
it is main activity and it was started by some other component, from
inside the same app or another app if security considerations permit it.
Also, activity
creation happens when you, for example, flip the screen
and the app needs to be built up with different screen characteristics.
During the creation process, the callback method
onCreate()
gets
called. You must implement this method since
there the GUI needs to
be built up. You can also use this callback method to start or connect to
services or provide content provider data. And you can use the APIs to
prepare
playing music, operating the camera, or doing anything else the
app is made for. This is also a good place to
initially set up a database
or other data storage your app needs.
Started
: Once done with the creation (and also in case of a restart after
a stop), the activity goes into the
started
state. Here the activity is about
to become visible to the user. During the start process, the callback
method
onStart()
gets called. This is a good
place to start broadcast
receivers, start services, and rebuild internal state and processes you
quit while the activity went to the stopped state.
Resumed
: Shortly before actually becoming visible to the user, the
activity goes through the resuming process. During that process the
callback
onResume()
gets called.
Running
:
The activity is fully visible, and the user can interact with it.
This state immediately follows the resuming process.