1

I've an activity register that have fragment container as viewpager using fragmentpageradapter. this kinda like a wizzard form with next and prev button .

In second fragment inside pager i try to get image from gallery with this code :

private void getAvaFromGallery() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_PICK);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), RESULT_LOAD_IMAGE);
}
@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode,data);
        if(resultCode == getActivity().RESULT_OK) {
            if (requestCode == RESULT_LOAD_IMAGE) {
                CompressBitmap cb = new CompressBitmap(getActivity());
                selectedImageUri = cb.handleImageUri(data.getData());
                String filepath = cb.getFilename();
                bitmap = cb.compressImage(cb.getRealPathFromURI(getActivity(), selectedImageUri), filepath);
                photo.setImageBitmap(bitmap);
                Log.d("FilePath", filepath);
            }
        }
    }

after getting data , i try to compress it into png and put it into imageview. but i think onactivityresult is not called in this case.

i've try with only fragment inside activity and this is work . but in my case fragment is inside fragment with pager adapter.

i hope someone can help me ..

Ibnu Habibie
  • 721
  • 1
  • 9
  • 20

1 Answers1

2

I was also facing a similar kind of issue. Below is the solution which worked for me. The code was for Draweractivity, but I was using a FragmentPagerAdapter in DrawerActivity.

  • First of all if you are calling the startActivityForResult() then don’t invoke that method directly. Instead call the getParentFragment().startActivityForResult() in the Child fragment.
  • Implement the onActivityResult() normally without any modifications in the Child fragment.
  • Now in the Main activity which is the parent for both parent and child fragments (in my case the Drawer activity) implement the below code in onActivityResult() which traverses the activity result to all it’s child fragments.

    for (Fragment fragment : getSupportFragmentManager().getFragments())
        {
            if (fragment != null)
            {
                fragment.onActivityResult(p_requestCode, p_resultCode, p_data);
            }
        }
    
  • In the Parent fragment’s onactivityresult(), we again need to implement the code to call the onActivityResult() for it’s child fragments.

List<Fragment> fragments = getChildFragmentManager().getFragments();
            if (fragments != null)
        {
            for (Fragment fragment : fragments)
            {
                Log.e("SupportFragment", fragment.getClass().getSimpleName());
                if(fragment instanceof ContactUsFragment)
                {
                    fragment.onActivityResult(p_requestCode, p_resultCode, p_data);
                }
            }
        }

Note: The code and the snippets is taken from the solution given here

Hope it helps...

  • I've tried ur code , and i get this error . I've another fragment that use the same activity for sign in by api facebook . Attempt to invoke interface method 'boolean com.facebook.CallbackManager.onActivityResult(int, int, android.content.Intent)' on a null object reference – Ibnu Habibie Jan 06 '16 at 17:09
  • The onActivityResult() method on which you are getting error is of Facebook. We are calling the onActivityResult() method of the activity. you need to have some mechanism to separate these callback methods –  Jan 07 '16 at 05:57