Space Ghost
Space Ghost

Reputation: 765

How does the Intent passed from the onActivityResult argument work with my application?

@Override
    public void onActivityResult(int reqCode, int resultCode, **Intent data**){
        super.onActivityResult(reqCode, resultCode, **data**);

        switch (reqCode) {
            case (PICK_CONTACT) :
                if (resultCode == Activity.RESULT_OK) {

                    Uri contactData = **data**.getData();
                    Cursor c =  getContentResolver().query(**contactData**, null, null, null, null);
                    if (c.moveToFirst()) {
                        String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        // TODO Whatever you want to do with the selected contact name.
                       //ImageView imageView = (ImageView)findViewById(R.id.imageView);
                       //Picasso.with(this).load("https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg").into(imageView);
                        FragmentManager FM = getFragmentManager();
                        FragmentTransaction FT = FM.beginTransaction();
                        FragmentActivity F1 = new FragmentActivity();



                        FT.add(R.id.frame_layout, F1);
                        FT.commit();

                    }
                }
                break;
        }

    }

Can someone explain how the data variable in the onActivityResult argument is used to make this code work?

I see that the variable is used to call getData() but I'm confused as to how this variable is connected to the Intent outside of this method. Furthermore, what exactly does calling data.getData() do ?

Basically I'm trying to understand this snippet of the code

 public void onActivityResult(int reqCode, int resultCode, Intent data){
        super.onActivityResult(reqCode, resultCode, data);

        switch (reqCode) {
            case (PICK_CONTACT) :
                if (resultCode == Activity.RESULT_OK) {

                    Uri contactData = data.getData();
                    Cursor c =  getContentResolver().query(contactData, null, null, null, null);

Could anyone help me make sense of it?

Upvotes: 0

Views: 530

Answers (3)

Trinimon
Trinimon

Reputation: 13967

Let's assume that the called Activity (started by startActivityForResult()) sets the result before finishing as follows:

Intent data = new Intent();
data.putExtra ("aValue", 42);
getActivity().setResult(Activity.RESULT_OK, data);
finish();

In order to get the value in the calling activity, use

public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case (PICK_CONTACT) :
            if (resultCode == Activity.RESULT_OK) {
                int aValue = data.getIntExtra("aValue", -1);
            ....
    }
}

This example is based on an int, however, there are a lot of other value types can be passed. See Androids Intent documentation.

p.s.: let me add that it works in the same way with void Intent.setData(Uri) and Uri Intent.getData() instead of putExtra(...) and getExtra(...)

Upvotes: 1

lcw_gg
lcw_gg

Reputation: 669

The data variable is the Intent that you have specified in the closing Activity. When you want to finish the Activity you called with startActivityForResult(), you should call setResult().

As first parameter, you should set the result code (RESULT_OK, RESULT_CANCELED, ...). You can also add a second one which should be an Intent and which will contain the information you want to receive in onActivityResult(). And that is the data variable in onActivityResult().

If you want to know more about getData(), you can check this : http://developer.android.com/reference/android/content/Intent.html#getData()

Upvotes: 1

TheTanic
TheTanic

Reputation: 1638

Take a look here : Click me and than the first answer

At the link you can see that you can give the Intent data a bundle or extras, which you get with your getData() method.

Upvotes: 1

Related Questions