Home > Android > android startActivity and startActivityForResult

android startActivity and startActivityForResult

In my previous tutorial I have talked a little bit about intent. So we have our intention to do something either explicitly or implicitly now it’s time to start those intent. And we do this via startActivity() or startActivityForResult() but why do we have two kind of method for this.

As name suggest startAcitvity will only take you there where you wanna go without worrying about getting any result from child activity to parent activity.
It’s real classic when you have a sequence activity to go through, like filling some information in every activity and that’s it.
What if you want an activity that uses information from other activity or may be other application. In that case you need more than just startActivity(). And that’s when comes startAcitvityForResult().
So it start another activity from your activity and it expect to get some data from child activity and return that to parent activity.
Let’s see some example:

final Bundle bundle = new Bundle();
al.add(1,un.getText().toString());
bundle.putStringArrayList("Q1", al);
Intent intent=new Intent(PageOne.this,PageTwo.class);
intent.putExtras(bundle);
startActivity(intent);

In this case I just wanna go from page one to page two and I don’t expect to get something from page two but I want page two to receive some data from page one and that I have sent using intent.putExtras().Full Code: ReportGenerator

Now we will try to understand startAcitvityForResult(). using previous ContactPicker example.
If you have not downloaded it yet you can download it from:
ContactPicker
At first we should be creating an intent in our main activity that is ContentPickerTester and also create a static integer variable
public static final int PICK_CONTACT=1;
now that need to be done bcz you don’t actually know which sub activity is going to give you the result(if you are using implicit intent) so we need to somehow differentiate between sub activities so that we can take different type of action for different type of result.
Intent intent=new Intent(Intent.ACTION_PICK,
Uri.parse("content://contacts/"));

startActivityForResult() accept two parameters as it’s arguments, one is going to be the explicit or implicit intent and another is called request code which is our static variable.
startActivityForResult(intent,PICK_CONTACT);
now whenever subactivity finishes, it will return the request code you have sent, along with the data and a result code to your main activity.

Let’s look at the subactivity (ContactPicker)
In the subactivity side subactivity will finish itself and return the data to the parent whenever it gets the chance to execute the below code.
Uri outUri=Uri.parse(data.toString()+rowId);
Intent outData=new Intent();
outData.setData(outUri);
setResult(Activity.RESULT_OK,outData);
finish();

Here Activity.RESULT_OK is the result code we were talking about. Now if something goes wrong with subactivity Activity.RESULT_CANCEL will return.

I don’t know if you have noticed or not that we are not calling ContactPicker anywhere in our main activity. Well we actually calling it implicitly. See at manifest file
That in our ContactPicker activity we have something like this:

<intent-filter>

<action android:name="android.intent.action.PICK"/>

<category android:name="android.intent.category.DEFAULT"/>

<data android:scheme="content" android:path="contacts"></data>

</intent-filter>

You should also remember that when we have created an intent in our main activity we defined our action as Intent.ACTION_PICK. And of course ContactPicker has an action named android.intent.action.PICK. And also the kind of data we are looking. Android will use something called Intent Resolution to find a best fit activity and that is in our case is ContactPicker activity.So voila, startActivityForResult() will open up ur subactivity.
After choosing a contact it will return to the main activity by setting the result setResult(Activity.RESULT_OK,outData) and finishing off the subactivity.

Now all you have to do is taking care of the returned data in the main activity and that is done by
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case(PICK_CONTACT):{
if(resultCode==Activity.RESULT_OK)
{
//blah blah…
}
break;
}
}
}

And that’s it. A real simple application. Awesome, isn’t it? And plz do comment or question , that will encourage me to write more. Also if there is any mistake let me know so that I can correct myself. And “HAVE A GOOD PROGRAMMING”

Advertisement
Categories: Android
  1. Aditya
    March 28, 2011 at 5:11 am | #1

    Thanks,
    Really Helpful..

    • saiful103a
      March 28, 2011 at 8:36 pm | #2

      Thanks bro. Need your feedback to improve.

  2. Max Waterman
    April 18, 2011 at 12:54 pm | #3

    I have an activity that I want to use in two modes; in one case it will not return anything, but in the other it will.
    Should I use ‘startActivityForResult()’ in both modes using a ‘mode’ parameter and make the ‘result’ empty in the mode where there is nothing to return?
    Or is there some way to tell that it has been started with ‘..ForResult()’ or plain ‘startActivity()’?

    • saiful103a
      April 20, 2011 at 12:12 pm | #4

      To return something to parent activity from child activity we must use startActivityForResult() even if it is null.But if we want to go to the next activity without caring about give something back to our previous activity we just use startActivity().And to give ur answer, yah u should use ‘startActivityForResult()’ in both modes using a ‘mode’ parameter bcz after all you have to return something or nothing. :)

  1. No trackbacks yet.

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.