且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

发送字符串从活动到另一个活动一个片段

更新时间:2022-12-19 10:28:26

首先,你实际上发送串到你的活动B.例如:

First, you'll actually send that string to your activity B. For example:

Intent intent = new Intent(this, YourActivityClass.class);
intent.putExtra("myString", "this is your string");
startActivity(intent);

然后再读取该字符串从你的活动B和执行片段交易之前注入到您的片段。例如:

then later read that string from your activity B and inject into your fragment before executing the fragment-transaction. For example:

Bundle args = new Bundle();
args.putString("myString", getIntent().getExtras().getString("myString"))
yourFragment.setArguments(args);

后来,使用 getArguments()在你的片段来检索包。

Later, use getArguments() in your fragment to retrieve that bundle.

或者,使用下面的在你的片段直接访问该活动意图并获取您需要的值:

Or alternatively, use the following in your fragment to directly access the activity intent and fetch your required value:

String str = getActivity().getIntent().getStringExtra("myString");

有关详细信息,请参阅

For more info, read this.