August
August

Reputation: 309

onActivityResult in fragment not called - possibly 'mixed' with other fragment?

I have followed this solution and few others but seems like I missed something, and I can't found it.

In Fragment A, when user clicked a button, they will choose a place in map.

PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
     startActivityForResult(builder.build(getActivity()), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
     e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
     e.printStackTrace();
}

After choose the place, still in the same fragment, the address will be in an EditText.

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d("checkCall", "here");
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == Activity.RESULT_OK) {
                // get the address
                Place place = PlacePicker.getPlace(getActivity().getApplicationContext(), data);
                String address = String.format("Place: %s", place.getAddress());
                Log.d("address", address);
                editTextBizAddress.setText(address);
            } else if (resultCode == Activity.RESULT_CANCELED){
                dialog("Error in getting the address");
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

So, in my parent activity - MainActivity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case 1001:
            if(resultCode == RESULT_OK){
                String tab = data.getExtras().getString("tab");
                if (tab.equals("profile")) {
                    viewPager.setCurrentItem(2);
                }
                tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        viewPager.setCurrentItem(tab.getPosition());
                        View focus = getCurrentFocus();
                        if (focus != null) {
                            hiddenKeyboard(focus);
                        }
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {

                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {
                        View focus = getCurrentFocus();
                        if (focus != null) {
                            hiddenKeyboard(focus);
                        }
                    }
                });
            }
            break;
        case 1:
            super.onActivityResult(requestCode, resultCode, data);
            break;
    }
}

I only managed to make it works only once. But after that, it doesn't work anymore. Though I have no idea how it can works once.

Is it because of the onActivityResult in MainActivity? Is it possible to get mixed up when other fragment also use onActivityResult in MainActivity?

EDIT: I have changed the onActivityResult in MainActivity since the requestCode have different values, but still the same - onActivityResult in fragment not called.

Upvotes: 0

Views: 273

Answers (2)

Vivek Mishra
Vivek Mishra

Reputation: 5705

For fragment to have a shot at receiving callback for onActivityResult, your activity must implement onActivityResult() and should have a call to super too.

For further explanations you can also see this answer.

Upvotes: 1

Artem Sborets
Artem Sborets

Reputation: 120

I assume the problem is about

super.onActivityResult...

in both activity and fragment.

Comment this line in activity. If it doesn't work - try opposite (remove it from fragment ant uncomment in activity)

Upvotes: 2

Related Questions