0

Here is my code hierarchy:
Activity1 ->> ROOT ACTIVITY
Fragment1 ->> inside Activity1
Viewpager ->> inside Fragment1
Fragment2 ->> hold by viewpager

now i want to select a picture from system gallary, so i use startActivityForResult() in Fragment2,but the onActivityResult() can never be called in Fragment2. here is my code in fragment2

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        btnlocalpic.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                OFNFragment.this.startActivityForResult(intent, 1);
            }
        });

        btntakepic_ofn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                OFNFragment.this.startActivityForResult(intent, 2);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("yancey", "onActivityResult  level3");
        Log.i("yancey", "requestCode is: " + requestCode);
        switch (requestCode) {
        case 1:
            Log.i("yancey", "uri is: " + data.getData());
            Uri uri = data.getData();
            ContentResolver resolver = getActivity().getContentResolver();
            try {
                Bitmap bitmap = BitmapFactory.decodeStream(resolver.openInputStream(uri));
                Intent intent = new Intent(getActivity(), PiceditActivity.class);
                intent.putExtra("bitmap", bitmap);
                getActivity().startActivity(intent);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            break;
        case 2:
            if(resultCode == Activity.RESULT_OK){
                String sdStatus = Environment.getExternalStorageState();
                if(!sdStatus.equals(Environment.MEDIA_MOUNTED)){
                    Log.i("TestFile", "SD card is not avaiable/writeable right now.");
                    return;
                }
                Bundle bundle = data.getExtras();
                Bitmap bitmap = (Bitmap)bundle.get("data");
                Intent intent = new Intent(getActivity(), PiceditActivity.class);
                intent.putExtra("bitmap", bitmap);
                startActivity(intent);
            }
            break;
        }
    }

hope you guys anderstand me, help me please!!

YanceyWang
  • 77
  • 1
  • 6

2 Answers2

2

Try to put onActivityResult in your parentActivity if callback comes there then you have to transfer that result to the fragment manualy by invoking onActivityResult of the desired Fragment from parent Activity.

Tell me if this works if you can understand this..or i will provide you a sample code..

Harry Sharma
  • 2,190
  • 2
  • 15
  • 41
1

It seems that nested fragments will not receive onActivityResult, not sure if it is a bug or it is the intended behaviour.

One thing you can do is to receive onActivityResult in your fragment one, then thank to the integer you pass to identify your request, you can call a method from your parent fragment to your child fragments through your ViewPager.

That should not be very difficult to do :)

Hope it helps

EDIT

Got from here, you can add methods to your FragmentPagerAdapter and ViewPager to get the currentFragment.

public Fragment getActiveFragment(ViewPager container, int position) {
    String name = makeFragmentName(container.getId(), position);
    return  mFragmentManager.findFragmentByTag(name);
}

private static String makeFragmentName(int viewId, int index) {
    return "android:switcher:" + viewId + ":" + index;
}

After this you can do the following in your onActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(requestCode == 1) //Here you SHOULD know that is from your childfragment
    {
         Fragment page = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":" + container.getCurrentItem());
 // based on the current position you can then cast the page to the correct 
 // class and call the method:
 if (container.getCurrentItem() == 0 && page != null) {
      ((MyChildFragment)page).callMethod(data);
       } 
    }
}

I have done this by heart so I might be missing something :)

Community
  • 1
  • 1
zozelfelfo
  • 3,776
  • 2
  • 21
  • 35
  • Please give more tips on **how to pass data form parent fragment to child fragments through ViewPager** here, thanks very much.
    BTW, it is a bug that nested fragments will not receive onActivityResult.
    – YanceyWang Dec 01 '14 at 08:27