Android 将数据从活动a传递到活动b,然后返回到活动a

Android 将数据从活动a传递到活动b,然后返回到活动a,android,Android,我想问一下如何将值从片段a传递到活动B,然后返回到片段。我尝试使用bundle传递值,但它会给出错误的数据 谢谢。如果要将数据从活动B发送到活动A,请使用代码 在活动B中 Intent intent = new Intent (); intent.putExtra ("string_1", "hello"); intent.putExtra ("string_2", "world"); intent.putExtra ("int_1", 1000); intent.putExtra ("long

我想问一下如何将值从片段a传递到活动B,然后返回到片段。我尝试使用bundle传递值,但它会给出错误的数据


谢谢。

如果要将数据从活动B发送到活动A,请使用代码

在活动B中

Intent intent = new Intent ();
intent.putExtra ("string_1", "hello");
intent.putExtra ("string_2", "world");
intent.putExtra ("int_1", 1000);
intent.putExtra ("long_1", 2000l);
activity.setResult (Activity.RESULT_OK, intent);
finish()
因此,在活动中,代码将是

@Override
protected void onActivityResult (int requestCode, int resultCode, Intent intent)
{
    if (resultCode == Activity.RESULT_OK)
    {
        String string_1 = intent.getStringExtra ("string_1", "");
        String string_2 = intent.getStringExtra ("string_2", "");
        int int_1 = intent.getIntExtra ("int_1", 0);
        long long_1 = intent.getLongExtra ("long_1", 0);
    }
}
现在你得到了A中的数据。 因此,通过使用fragment中定义的方法,您可以将数据发送到目标fragment

喜欢说

在片段中,如果您定义

public void refreshFragment(String s1)  {

// here s1 will be the data you send from activity Q
}
希望这有帮助