Starting an Android Activity
by, 02-11-2012 at 10:53 AM (819 Views)
startActivity() method is called when a new activity is started. Intent object is passed as parameter and it describes your activity. What actions you want to perform are described by this intent. Also data used by an activity is carried by this intent.
For launching a new activity, create an intent that describes the activity using the class name. Following code explains how one activity can start a new activity.
There are cases in which you want to perform different actions like:Java Code: This is the code to create a new IntentIntent intent = new Intent(this, SignInActivity.class); startActivity(intent);
- Send email
- Text message
- Status update
In such situation, you can leverage the activities. These are provided by different applications on the device. You need to create an intent that describes the action for your activity and system will launch the activity from required application. In case of more than one activity, user can select the most suitable option.
To send an email message, following intent is created.
In the above code snippet, EXTRA_EMAIL is added which is a string array of email addresses. Email will be send to these addresses. In response to this intent, an email application will read the string array and will place these addresses into “to” field.Java Code: This is the code to set and start activityIntent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_EMAIL, recipientArray); startActivity(intent);