【发布时间】:2017-03-11 06:05:50
【问题描述】:
我试图将一个值从我的Activity 传递给我的Fragment,但Bundle 始终为空。
活动
CallLogsFragment callLogfrag = new CallLogsFragment();
Intent intent = new Intent(this, DeviceUsageActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("key", callgroup);
callLogfrag.setArguments(bundle);
this.startActivity(intent);
用于从Activity检索值的片段
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
bundle = this.getArguments();
if (bundle != null) {
int myInt = bundle.getInt("key", 0);
}
View view = inflater.inflate(R.layout.fragment_call_logs, container, false);
load(view);
setupList(view);
return view;
}
【问题讨论】:
-
那么显然 onCreateView 没有在他在这里设置的片段中被调用(也许是来自活动布局的片段?)
-
DeviceUsageActivity.class 是活动或片段
-
@zidane 你能回复一下吗,如果你正在开始一个活动,那么你应该把意图附加内容放在你的活动中,并使用 getIntent().getExtras.get(key)
-
你为什么要
this.startActivity(intent)?这将启动一个新的 Activity。你不想附加一个片段吗?
标签: java android android-studio android-fragments android-activity