Java Reference
In-Depth Information
Incontrast,
onDestroy()
isusuallyimplementedtofreeenvironmentalresources
(e.g., threads) that are associated with an activity so that a destroyed activity doesn't
leave such things around while the rest of its app is still running.
specifically,theactivityisstartedbycreatingan
Intent
objectdescribinganexplicit
or implicit intent, and by passing this object to
Context
's
void startActiv-
ity(Intent intent)
method(launchanewactivity;noresultisreturnedwhenit
finishes).
Alternatively, the activity could be started by calling
Activity
's
void
startActivityForResult(Intent intent, int requestCode)
meth-
od. The specified
int
result is returned to
Activity
's
void onActivityRes-
ult(int requestCode, int resultCode, Intent data)
callbackmeth-
od as an argument.
Note
The responding activity can look at the initial intent that caused it to be
launched by calling
Activity
's
Intent getIntent()
method. Android calls
theactivity's
void onNewIntent(Intent intent)
method(alsolocatedinthe
Activity
class) to pass any subsequent intents to the activity.
SimpleActivity
serving as its main activity, let's assume that this app includes a
SimpleActivity2
class describing an activity for viewing JPEG images. Suppose
that you want to start
SimpleActivity2
from
SimpleActivity
's
onCreate(Bundle)
method.Thefollowingexampleshowsyouhowtoaccomplish
this task:
Intent intent = new Intent(SimpleActivity.this, Sim-
pleActivity2.class);
SimpleActivity.this.startActivity(intent);
Thefirstlinecreatesan
Intent
objectthatdescribesanexplicitintent.Itinitializes
this object by passing the current
SimpleActivity
instance's reference and
Sim-
pleActivity2
's
java.lang.Class
instancetothe
Intent(Context pack-
ageContext, Class<?> clazz)
constructor.
Thesecondlinepassesthis
Intent
objectto
startActivity(Intent)
,which
is responsible for starting the activity described by
SimpleActivity2.class
.
If
startActivity(Intent)
was unable to find the specified activity (which
shouldn't
happen),
it
would
throw
an
an-
droid.content.ActivityNotFoundException
instance.