gpt4 book ai didi

java - 如何从 onActivityResult 从 Activity 移动到 Fragment

转载 作者:行者123 更新时间:2023-12-02 03:11:48 25 4
gpt4 key购买 nike

我是 Fragment Activity 的新手,我的情况是我已经实现了支付网关并成功获得了 Activity 的结果。我的确认 Activity 位于 fragment 中,因此当我从 Activity 中的支付网关收到结果时,在 PG 的成功或失败条件后,在 onActivityResult 中如何转移到确认 fragment 我正在使用android.app.Fragment;和我的 PaymentActivity 代码

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

super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PayUmoneySdkInitilizer.PAYU_SDK_PAYMENT_REQUEST_CODE) {





if (resultCode == RESULT_OK) {
Log.i(TAG, "Success - Payment ID : " + data.getStringExtra(SdkConstants.PAYMENT_ID));
String paymentId = data.getStringExtra(SdkConstants.PAYMENT_ID);

} else if (resultCode == RESULT_CANCELED) {
Log.i(TAG, "failure");
showDialogMessage("cancelled");
} else if (resultCode == PayUmoneySdkInitilizer.RESULT_FAILED) {
Log.i("app_activity", "failure");

if (data != null) {
if (data.getStringExtra(SdkConstants.RESULT).equals("cancel")) {

} else {
showDialogMessage("failure");
}
}
//Write your code if there's no result
} else if (resultCode == PayUmoneySdkInitilizer.RESULT_BACK) {
Log.i(TAG, "User returned without login");
showDialogMessage("User returned without login");
}
}
}

在上面的代码中,我有一些条件,因此根据这些条件我必须移动 fragment 。

我有一些确认 fragment 。我希望你理解或者给我更好的方法

我的主要 Activity 代码

private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new BuyActivity();

break;
case 2:
fragment = new SaleFragment();
break;
case 3:
fragment = new MoneyFragment();
break;
case 4:
//fragment = new CommunityFragment();
break;
case 5:
//History
fragment = new HistoryFragment();
break;
case 6:
//about
fragment = new AboutFragment();
break;

default:
break;
}

if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();

// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}

谢谢

最佳答案

您必须在 MainActivity.Java 中编写 onActivityResult(),如下所示

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
for (Fragment fragment : getChildFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}

在此之后, fragment 中的 onActivityResult 被调用。

关于java - 如何从 onActivityResult 从 Activity 移动到 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40925250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com