如何将onAcitivtyResult用于Android中的一些片段

问题描述 投票:1回答:3

在我的应用程序中,我有一个activity(MainActivity)和一些fragments(ServicesFragment,NewAddressFragment)。 在viewPager和NewAddressFragment中使用的ServicesFragment使用getFragmentManager().replace ...来显示这个fragments! 在这个fragments我使用onActivityResult,但在fragments之一发生冲突而不是onActivityResult之一的fragments代码的调用代码。 我写下面的代码,请看这个。 主要活动 :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Fragment servicesFrag = bottomNavAdapter.getItem(mainViewPager.getCurrentItem());
    if (servicesFrag instanceof ServicesFragment) {
        ((ServicesFragment) servicesFrag).onActivityResult(requestCode, resultCode, data);
    }
}

ServiceFragment:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.e("BazaarPaymentLog", "Result");
    Log.e("BazaarPaymentLog", "bazaarRequestCode : " + bazaarRequestCode);
    if (iabHelper == null) return;

    if (requestCode == bazaarRequestCode) {
        String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
        if (resultCode == RESULT_OK) {
            try {
                JSONObject jo = new JSONObject(purchaseData);
                purchaseToken = jo.getString("purchaseToken");
                productId = jo.getString("productId");
                packageName = jo.getString("packageName");

                Log.e("BazaarPaymentLog", "purchaseToken : " + purchaseToken);
                if (iabHelper != null) {
                    iabHelper.handleActivityResult(requestCode, resultCode, data);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

NewAddressFragment:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_TAKE_PHOTO || requestCode == REQUEST_PICK_PHOTO) {
            if (data != null) {
                // Get the Image from data
                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                assert cursor != null;
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                mediaPath = cursor.getString(columnIndex);
                cursor.close();

                postPath = mediaPath;
                uploadImageToServer(postPath);
            }

        } else if (requestCode == CAMERA_PIC_REQUEST) {
            if (Build.VERSION.SDK_INT > 21) {
                postPath = mImageFileLocation;
            } else {
                postPath = fileUri.getPath();
            }
            uploadImageToServer(postPath);
        }
    } else if (resultCode != RESULT_CANCELED) {
        Toast.makeText(getActivity(), "Failed", Toast.LENGTH_LONG).show();
    }
}

当在onActivityResult中写MainActivity代码时,在ServicesFragment中调用onActivityResult代码并在NewAddressFragment中调用onActivityResult代码。

但是当在onActivityResult中删除MainActivity代码时,在ServicesFragment中不调用onActivityResult代码而在NewAddressFragment中调用onActivityResult代码。

我该如何解决?

java android android-fragments onactivityresult
3个回答
2
投票

在MainActivity的onActivityResult中使用:

而不是: -

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment servicesFrag = bottomNavAdapter.getItem(mainViewPager.getCurrentItem());
if (servicesFrag instanceof ServicesFragment) {
    ((ServicesFragment) servicesFrag).onActivityResult(requestCode, resultCode, data);
}

}

用这个 :-

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

    for(Fragment fragment : getSupportFragmentManager().getFragments()){
        fragment.onActivityResult(requestCode,resultCode,data);
    }

}

让我知道它是否有效


0
投票

请在活动中将此行添加到onActivityResult

    Fragment servicesFrag = bottomNavAdapter.getItem(mainViewPager.getCurrentItem());
 Fragment addressFrag = findFragmentByTag("NewAddressFragmentTag");
 if (servicesFrag instanceof ServicesFragment) {
 ((ServicesFragment) servicesFrag).onActivityResult(requestCode, resultCode, data);
 }
 if (addressFrag instanceof NewAddressFragment) {
 ((NewAddressFragment) addressFrag).onActivityResult(requestCode, resultCode, data);
 }
 }

0
投票

我可以解决我的问题! 使用以下代码更改MainActivity代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Fragment servicesFrag = bottomNavAdapter.getItem(mainViewPager.getCurrentItem());
    Fragment addressFrag = getSupportFragmentManager().findFragmentByTag("NewAddressFragmentTag");
    if (servicesFrag instanceof ServicesFragment) {
        ((ServicesFragment) servicesFrag).onActivityResult(requestCode, resultCode, data);
    }

    if (addressFrag instanceof NewAddressFragment) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

我希望能为其他亲爱的开发者提供帮助

© www.soinside.com 2019 - 2024. All rights reserved.