[英]Pass data from Activity to Fragment in another Activity
[英]Passing data from fragment in an Activity from Another Activity
我研究过将数据从Activity传递给Activity。 但我无法使用相同的方法传递给我的片段。 如何将数据传递给我的片段?
我正在使用的准则是
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);
在另一项活动中
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
使用setArguments()
将Bundle
传递给Fragment。 即使在配置更改时,Fragment也会保留此捆绑包。 它将被传递给onCreate()
和其他片段方法。
通过其id或标记查找现有片段: getFragmentManager.findFragmentById()
,并调用片段类的方法。
将父活动引用保存在片段的onAttach()
中的局部变量中,并从片段调用父活动类的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.