Skip to content

Android: startActivity and startActivityForResult

June 14, 2012

Here are some differences between startActivity and startActivityForResult

 

1. startActvity()

startActivity() will start the activity you want to start without worrying about getting any result from new child activity started by startActivity to parent activity.

For use of startActivity() with intents read Android Intents

2. startAcitvityForResult()

startAcitvityForResult() starts another activity from your activity and it expect to get some data from newly started child activity  by startAcitvityForResult()  and return that to parent activity.

Example:

To start an activity ( for example, MySecondActivity ) using startAcitvityForResult() following statements can be used

 

private static final int SECOND_ACTIVITY=0;
Intent myIntent = new Intent(this, MySecondActivity.class);
startActivityForResult(myIntent, SECOND_ACTIVITY);

The statements above creates an intent that is used to initiate the activity MySecondActivity.

startActivityForResult ( ) has two parameters, first is

Intent myIntent, which is used to start MySecondActivity. And second parameter is
request code SECOND_ACTIVITY which is returned to our calling activity through a call to
onActivityResult(int requestCode, int resultCode, Intent intent)

onActivityResult ( ) has three parameters passed to it, first is requestCode which was provided through startActivityForResult ( ). This request code is used to recognize the activity in case there are more than one child activites started through startActivityForResult ( ) method.
Second parameter to onActivityResult ( ) is resultCode which is used to determine the result of activity as completed or cancelled etc.
And the third parameter to onActivityResult ( ) is intent through which different type of data can be attached by the child activity.

I will provide detailed examples in my next upcoming posts.

Advertisements

From → Android

3 Comments
  1. abdullah permalink

    very nice website

Trackbacks & Pingbacks

  1. Application crashing when relocating from activity to another - Zsohar

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: