gpt4 book ai didi

java - 是否可以在 onActivityResult() 上 setArguments() 并将其传递给 Fragment?

转载 作者:行者123 更新时间:2023-12-01 18:03:23 25 4
gpt4 key购买 nike

Is it possible to setArguments() on onActivityResult() and pass it to FragmentActivity?

从我的 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();
}

我在ActivityResult()上有这段代码。

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");

}

最佳答案

这是什么?

SORT_VALUE = args.getString("SORT_VALUE");
SORT_BY = args.getString("SORT_BY");

您尝试在常量中写入字符串吗?
尝试类似的事情

String sortValue = args.getString("SORT_VALUE");

了解有关 bundle 使用的更多信息 here
但通常需要 bundle 在 fragment 之间传递数据
用于 Activity 使用

Intent intent = new Intent(getBaseContext(), NewActivity.class);
intent.putExtra("YOUR_CONSTANT", yourData);
startActivity(intent);

访问下一个 Activity 的 Intent :

String sessionId = getIntent().getStringExtra("YOUR_CONSTANT");

关于java - 是否可以在 onActivityResult() 上 setArguments() 并将其传递给 Fragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60597073/

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