【发布时间】:2020-06-21 03:18:00
【问题描述】:
是否可以在 onActivityResult() 上设置 Arguments() 并将其传递给 片段活动?
从我的 SecondActivity,我通过 onBackPressed() 将一个包传递给我的 FirstActivity。
@Override
public void onBackPressed() {
Bundle bundle = new Bundle();
bundle.putString("SORT_VALUE", SORT_BY_VALUE);
bundle.putString("SORT_BY", sortby);
bundle.putString("FROM_SORT", "FROM_SORT");
Intent intent = new Intent();
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
back();
super.onBackPressed();
}
我有这段代码 onActivityResult()。
if (requestCode == SORT_RESULT) {
if (data != null && data.getExtras() != null) {
Bundle bundle = data.getExtras();
SORT_VALUE = bundle.getString("SORT_VALUE");
sortby = bundle.getString("SORT_BY");
FILTER_VALUE = bundle.getString("FILTER_VALUE");
filterby = bundle.getString("FILTER_BY");
final ProductListFragment frg = new ProductListFragment();;
Bundle extra = new Bundle();
extra.putString("SORT_VALUE", SORT_VALUE);
extra.putString("SORT_BY", sortby);
frg.setArguments(extra);
if (SORT_VALUE != null) {
if (SORT_VALUE.equalsIgnoreCase("Popularity")) {
setSortValueAndSortBy("Popularity", "fmcg-campaign.status=active,-monthly-popular-score");
}
//this contains code for other options
}
这是我的 FragmentActivity 代码,它调用方法 onCreate() 但值在我结束时返回 null。
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
SORT_VALUE = args.getString("SORT_VALUE");
SORT_BY = args.getString("SORT_BY");
}
【问题讨论】:
-
您尝试将这些数据传入多少个活动,您提到了 SecondActivity、FirstActivity 和 FragmentActivity。最后两个应该是一样的。或者您是否尝试将此值传递给
ProductListFragment,在这种情况下,您应该获取片段内的值而不是活动。一方面,如果 Activity 的onCreate已经创建,则不会调用它,其次,传递到片段中的包应该在片段而不是其活动中调用