Starting an Activity

What is an Activity?

An activity in android is the user interface that is used to interact with the user. Simply said any android application is made up of one or more activities(screens). The activity is made up of Viewgroups and views. Viewgroups are basically layout for the screen eg. Linearlayout is used to layout the widgets (buttons, text-box etc. ) in a linear manner ie. either horizontally or vertically.   Any activity could start any other activity.

How to start?

Its very important to declare the activity in the android manifest file (AndroidManifest.xml) to make it visible to the system. The declaration looks something like this:

You can start another activity by calling startActivity(), passing it an Intent that describes the activity you want to start. The intent is the heart of the android system it is used to start activities , services and  broad cast receivers. The intent specifies either the exact activity you want to start or describes the type of action you want to perform (and the system selects the appropriate activity for you, which can even be from a different application). An intent can also carry small amounts of data to be used by the activity that is started.

When working within your own application, you’ll often need to simply launch a known activity. You can do so by creating an intent that explicitly defines the activity you want to start, using the class name. For example, here’s how one activity starts another activity named SignInActivity:

Intent intent = new Intent(this, SignInActivity.class);

startActivity(intent);

However, your application might also want to perform some action, such as send an email, text message, or status update, using data from your activity. In this case, your application might not have its own activities to perform such actions, so you can instead leverage the activities provided by other applications on the device, which can perform the actions for you. This is where intents are really valuable—you can create an intent that describes an action you want to perform and the system launches the appropriate activity from another application. If there are multiple activities that can handle the intent, then the user can select which one to use. For example, if you want to allow the user to send an email message, you can create the following intent:

Intent intent = new Intent(Intent.ACTION_SEND);

intent.putExtra(Intent.EXTRA_EMAIL, recipientArray);

startActivity(intent);

The EXTRA_EMAIL extra added to the intent is a string array of email addresses to which the email should be sent. When an email application responds to this intent, it reads the string array provided in the extra and places them in the “to” field of the email composition form. In this situation, the email application’s activity starts and when the user is done, your activity resumes.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s