我想在片段之间传递信息。为此,我在我的BoothDetailFragment中创建了一个Bundle,我想在GridFragment中打开它。这样做以前在其他Fragments中都可以使用,但是现在在OnCreateView中调用参数时,它们始终为null。
我读过其他线程,建议检查是否在xml中定义了id,在我的项目中就是这种情况。
在这里创建捆绑包
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putSerializable("booth", booth);
getMainActivity().showFragment(FragmentType.GRID, bundle);
}
这是MainActivity中的showFragment方法
public void showFragment(FragmentType fragmentType, Bundle fragArgs) {
showFragment(fragmentType, getSupportFragmentManager(), fragArgs);
}
public static void showFragment(FragmentType fragmentType, FragmentManager fm, Bundle fragArgs) {
String fragmentTag = fragmentType.name();
Fragment fragTarget = fm.findFragmentByTag(fragmentTag);
//
if (fragTarget != null || fragmentType.mainScreen) {
// pop the stack until our target is on top, or we are at root level.
popBackstackUntil(fm, fragmentTag);
} else if (fragmentType == FragmentType.DEBUG_LOG || fragmentType == FragmentType.DEVICE_ERRORS) {
// try to find the other one
String otherFragmentTag;
if (fragmentType == FragmentType.DEBUG_LOG) {
otherFragmentTag = FragmentType.DEVICE_ERRORS.name();
} else {
otherFragmentTag = FragmentType.DEBUG_LOG.name();
}
Fragment otherFragment = fm.findFragmentByTag(otherFragmentTag);
if (otherFragment != null) {
// pop the backstack up to and including the other fragment
boolean b = popBackstackUntil(fm, otherFragmentTag);
if (Constants.DEBUG) {
Preconditions.checkState(b, "FIXME");
}
// one more pop to remove the found other-fragment
fm.popBackStack();
}
}
// start transaction to modify the ActionBar, Footer, and if necessary,
// the main container as well...
final FragmentTransaction ft = fm.beginTransaction();
if (fragTarget == null) {
fragTarget = createNewFragmentInstance(fragmentType, fragArgs);
ft.replace(R.id.content_main, fragTarget, fragmentTag);
}
if (!fragmentType.mainScreen) {
ft.addToBackStack(fragmentTag);
}
ft.commit();
}
在这里,我尝试在GridFragment中打开捆绑包
View view = inflater.inflate(R.layout.fragment_grid, container, false);
ButterKnife.bind(this, view);
Bundle bundle = getArguments();
CreateNewFragmentInstance方法
if (Constants.DEBUG) {
log.d("createFragment " + fragmentType);
}
AbstractArgoFragment frag = fragmentType.newInstance();
if (fragArgs != null) {
frag.setArguments(fragArgs);
}
return frag;
该束束每次都为空