本文介绍了片段 getArguments(在 onResume 中)返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将一些数据从 Activity 发送到 Fragment.
I am trying to send some data from a Activity to Fragment.
我需要在片段的 onResume 方法中获取数据,但我想这是不可能的?
I need to get the data in the onResume method of the fragment but I guess that`s not possible?
参数只能在 onCreate() 中接收?
Arguments can only be received in onCreate()?
活动:
public void someMethod() {
String someString = "test";
Bundle bundle = new Bundle();
bundle.putString("message", someString);
VFragment.getInstance().setArguments(bundle);
}
片段:
public class VFragment extends BaseFragment {
public static VFragment getInstance() {
return VFragment_.builder().build();
}
public VFragment() {
}
@Override
public void onResume() {
super.onResume();
String receive = getArguments().getString("message");
Log.v(TAG, receive); // NULL
}
}
推荐答案
好吧,我不知道什么是 builder()
和 build
但这是一个很好的做法...
Alright I don't know what is builder()
and build
but this is good practice...
public static VFragment newInstance(String text) {
Bundle b = new Bundle();
b.putExtrs("message", text)
VFragment mF = new VFragment();
mF.setArguments(b);
return mF;
}
试试这个.参考
这篇关于片段 getArguments(在 onResume 中)返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!