android startactivityforresult request code

Android-startactivityforresult-request-code-featured.jpg

android startActivity and startActivityForResult

Within my previous tutorial I’ve spoken a bit about intent. Therefore we have our intention to behave either clearly or unconditionally now you have to begin individuals intent. And that we do that via startActivity() or startActivityForResult() why will we have two type of way of this.

As title suggest startAcitvity is only going to get you there in which you wanna go without having to worry about getting any derive from child activity to parent activity.
It’s real classic if you have a string activity to undergo, like filling some good info in each and every activity and that is it.
What if you would like a task that utilizes information using their company activity or might be other application. For the reason that situation you’ll need not only startActivity(). And that is when comes startAcitvityForResult().
Therefore it start another activity out of your activity also it be prepared to acquire some data from child activity and return that to parent activity.
Let’s wait and watch some example:

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

Within this situation I simply wanna move from the first page to page two and that i pricier to obtain something from page two however i want page two to get some data from the first page which I’ve sent using intent.putExtras().Full Code: ReportGenerator

Now we’ll attempt to understand startAcitvityForResult(). using previous ContactPicker example.
Should you haven’t downloaded it yet you are able to download it from:
ContactPicker
In the beginning you should be creating an intent within our primary activity that’s ContentPickerTester as well as produce a static integer variable
public static final int PICK_CONTACT=1
now that should be done bcz you do not really know which sub activity will provide you with the result(if you work with implicit intent) so we have to in some way differentiate between sub activities to ensure that we are able to take different kind of action for various kind of result.
Intent intent=new Intent(Intent.ACTION_PICK,
Uri.parse(“content://contacts/”))
startActivityForResult() accept two parameters as it is arguments, one will probably be the explicit or implicit intent and the other is known as request code that is our static variable.
startActivityForResult(intent,PICK_CONTACT)
now whenever subactivity finishes, it’ll return the request code you’ve sent, together with the information along with a result code for your primary activity.

Let us consider the subactivity (ContactPicker)
Within the subactivity side subactivity will finish itself and return the information towards the parent whenever it will get the opportunity 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 may be the result code i was speaking about. Now if something wrong happens with subactivity Activity.RESULT_CANCEL will return.

I’m not sure for those who have observed or otherwise that we’re not calling ContactPicker any place in our primary activity. We really calling it unconditionally. See at manifest file
That within our ContactPicker activity we’ve something similar to this:

It’s also wise to keep in mind that whenever we have produced an intent within our primary activity we defined our action as Intent.ACTION_PICK. Not to mention ContactPicker comes with an action named android.intent.action.PICK. As well as the type of data we’re searching. Android uses something known as Intent Resolution to locate a best fit activity which is within our situation is ContactPicker activity.So voila, startActivityForResult() will open ur subactivity.
After selecting a message it’ll go back to the primary activity by setting the end result setResult(Activity.RESULT_OK,outData) and completing the subactivity.

Now all you need to do is being careful from the came back data within the primary activity which is performed by
protected void onActivityResult(int requestCode, int resultCode, Intent data) situation(PICK_CONTACT):
break

And that is it. A genuine simple application. Awesome, is not it? And plz do comment or question , which will encourage me to create more. And if there’s any mistake tell me to ensure that I’m able to correct myself. And &ldquoHAVE A Great PROGRAMMING&rdquo

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>