[英]Passing data from fragment in an Activity from Another Activity
I have researched on passing data from Activity to Activity. 我研究过将数据从Activity传递给Activity。 But I could not use the same method to pass to my fragment. 但我无法使用相同的方法传递给我的片段。 How do I pass the data to my fragment? 如何将数据传递给我的片段?
The Code I am using is 我正在使用的准则是
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);
and In the other activity 在另一项活动中
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
Use setArguments()
to pass a Bundle
to a Fragment. 使用setArguments()
将Bundle
传递给Fragment。 Fragment will retain this bundle even on configuration change. 即使在配置更改时,Fragment也会保留此捆绑包。 It will be delivered to onCreate()
and other methods of fragment. 它将被传递给onCreate()
和其他片段方法。
Find an existing fragment by its id or tag: getFragmentManager.findFragmentById()
, and call fragment class's method. 通过其id或标记查找现有片段: getFragmentManager.findFragmentById()
,并调用片段类的方法。
Save parent activity reference to a local variable in onAttach()
of fragment and call parent activity class's methods from fragment. 将父活动引用保存在片段的onAttach()
中的局部变量中,并从片段调用父活动类的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.