仅替换片段而不传递数据
Replace fragment only without passing data
我正在尝试打开我的第二个列表片段,但由于下面的代码不完善,它认为我想从 DummyContent 传递数据以显示 'Item x',而实际上我并不想。在 if and/or else 语句中需要更改什么,以便列表片段打开而不是 'Item x' 出现?我知道 ARG_ITEM_ID
是罪魁祸首,但我不知道将其更改为什么。
@Override
public void onItemSelected(String id) {
if("1".equals(id)){
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
ContinentsListFragment fragment = new ContinentsListFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_list, fragment)
.commit();
} else {
Intent detailIntent = new Intent(this, ContinentsListActivity.class);
detailIntent.putExtra(ContinentsListFragment.ARG_ITEM_ID, id);
startActivity(detailIntent);
}
}
}
这似乎太简单了,但让我们开始了解这个问题。
示例代码修复:
Intent detailIntent = new Intent(this, ContinentsListActivity.class);
startActivity(detailIntent);
备注:
- 带有 putExtra 的代码已删除。
- 如果您坚持调用 putExtra,请使 ContinentsListFragment.ARG_ITEM_ID static 访问或使用任何技术上可访问的常量。
我正在尝试打开我的第二个列表片段,但由于下面的代码不完善,它认为我想从 DummyContent 传递数据以显示 'Item x',而实际上我并不想。在 if and/or else 语句中需要更改什么,以便列表片段打开而不是 'Item x' 出现?我知道 ARG_ITEM_ID
是罪魁祸首,但我不知道将其更改为什么。
@Override
public void onItemSelected(String id) {
if("1".equals(id)){
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
ContinentsListFragment fragment = new ContinentsListFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_list, fragment)
.commit();
} else {
Intent detailIntent = new Intent(this, ContinentsListActivity.class);
detailIntent.putExtra(ContinentsListFragment.ARG_ITEM_ID, id);
startActivity(detailIntent);
}
}
}
这似乎太简单了,但让我们开始了解这个问题。 示例代码修复:
Intent detailIntent = new Intent(this, ContinentsListActivity.class);
startActivity(detailIntent);
备注:
- 带有 putExtra 的代码已删除。
- 如果您坚持调用 putExtra,请使 ContinentsListFragment.ARG_ITEM_ID static 访问或使用任何技术上可访问的常量。