且构网

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

从Android的另一个活动将数据发送到片段

更新时间:2023-01-08 07:51:20

您需要使用 startActivityForResult()来开始你的第二个活动。在你的第二个活动,你完成它之前,你需要将数据添加到包通过这个来的意图,然后将结果的意图。

You would need to start your second activity using startActivityForResult(). In your second activity before you finish it, you need to add the data to a bundle pass this to an intent and then set the result to the intent.

Bundle bundle = new Bundle();
bundle.putString("myData", "myValue");
Intent intent = new Intent();
intent.putExtra(bundle);
setResult(intent, 0);
finish();

然后在活动1应该有一个 onactivityresult 方法,从目的检索值,并将它放在你想在你的片段

And then in activity 1 there should be an onactivityresult method which retrieves the value from the intent and sets it where you want in your fragment

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bundle bundle = data.getData();
string value = bundle.getString("myData");
}

我不知道如果我有完全正确的,在我的头顶记住它,但应该足以让你开始,我认为。

I'm not sure if I have it exactly right as remembering it at the top of my head but should be enough to get you started I think.