3

In my fragment onActivityResult is not calling after startActivityForResult .As i have tried following things: 1) adding getParentFragment , getActivity before startActivityResult

2) added on manifest android:configChanges="orientation|screenSize"

3)onActivityResult is not being called in Fragment

4)Checked finish () method in ZbarScanner Activity

Intent dataIntent = new Intent();
                    dataIntent.putExtra(SCAN_RESULT, symData);
                    dataIntent.putExtra(SCAN_RESULT_TYPE, sym.getType());
                    setResult(Activity.RESULT_OK, dataIntent);
                    finish();

5) Error- [ResultInfo{who=null, request=196609, result=-1, data=Intent { (has extras) }}]} checked intent has value not null which we are passing

if (isCameraAvailable()) {
                    Intent intent = new Intent(this.getActivity(), ZBarScannerActivity.class);
                    intent.putExtra(ZBarConstants.SCAN_MODES, new int[]{Symbol.QRCODE});

                  startActivityForResult(intent, 1);


                    Toast.makeText(getActivity(), "inside case", Toast.LENGTH_SHORT).show();


                } else {
                    Toast.makeText(getActivity(), "Rear Facing Camera Unavailable", Toast.LENGTH_SHORT).show();
                }

----onActivityResult------

  @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {

            super.onActivityResult(requestCode, resultCode, data);
            Bundle bundle = data.getExtras();


            switch (requestCode) {
                case ZBAR_SCANNER_REQUEST:
                case ZBAR_QR_SCANNER_REQUEST:
                    if (resultCode == getActivity().RESULT_OK) {


                        Toast.makeText(getActivity(), "Scan Result = " + data.getStringExtra(ZBarConstants.SCAN_RESULT), Toast.LENGTH_SHORT).show();

                    } else if (resultCode == getActivity().RESULT_CANCELED && data != null) {
                        String error = data.getStringExtra(ZBarConstants.ERROR_INFO);
                        if (!TextUtils.isEmpty(error)) {
                            Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show();
                        }
                    }}
Community
  • 1
  • 1
Poonam Kukreti
  • 359
  • 5
  • 19

3 Answers3

2

I called onActivityResult on MainActivity and with this its not calling .so removed switch case .and its working now

switch (requestCode) {
            case ZBAR_SCANNER_REQUEST:
            case ZBAR_QR_SCANNER_REQUEST:
Poonam Kukreti
  • 359
  • 5
  • 19
0

It is very common and known issue for fragment onActivityResult. I have solved it in one way, there might be other ways also.

set tag when you are moving to Fragment from Activity like this.

fragmentManager.beginTransaction().replace(R.id.frame_container, fragment,"tag").commit();

now write below code in your onActivityResult of Activity.

 if (requestCode == 1) { //set your requestcode whichever you are using
        //super.onActivityResult(requestCode, resultCode, data); 
        //dont forget to comment this.

        Fragment fragment = (Fragment) getSupportFragmentManager().findFragmentByTag("tag");
        if(fragment != null){
            fragment.onActivityResult(requestCode, resultCode, data);
        }
}
Ravi
  • 34,851
  • 21
  • 122
  • 183
  • As we are not replacing it . We are calling this fragment like this in tabs class MyAdapter extends FragmentPagerAdapter { public MyAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch (position){ case 0 : return new FirstFragment(); case 1 : return new SecondFragment(); case 2 : return new ThirdFragment(); } return null; }so is there any way to call the fragment as you mentioned above? – Poonam Kukreti Jan 27 '16 at 06:16
0

I had a similar issue while working with a FragmentPagerAdapter inside a Fragment. The problem was that I initialized my PagerAdapter with the Fragment's FragmentManager, I replaced:

new MyPagerAdapter(getChildFragmentManager());

with

new MyPagerAdapter(getFragmentManager());

So it might be worth checking which FragmentManager you are using. I'm not 100% happy with this approach yet as it seems right to call the Fragment's FragmentManager. But I did not get the time to check it any further yet.

erik
  • 1
  • 1