伙计们,我正在处理一个android项目,我需要将托管BottomNavigationView的父活动的数据传递给它的子片段。我试着在网上遵循一些解决方案,但我的问题似乎很特殊,因为它涉及BottomNavigationView
RhemaHiveMessagingClientActivity.java
在我的onCreate((内部
try{
messageBundle = new Bundle();
rhemFrag = new RhemaHiveMessageFragment();
messageBundle.putString("user_church", churchName);
rhemFrag.setArguments(messageBundle);
getAuto().getToast(c, "Church Passed is " + churchName, RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
}catch(NullPointerException np){
getAuto().getToast(c, RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + np.getLocalizedMessage(),RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
}
RhemaHiveMessageFragment.java
在我的onCreateView((中
churchBund = getArguments();
if(churchBund.containsKey("user_church")){
churchName = churchBund.getString("user_church");
getAuto().getToast(getContext(),"Church Recieved : " + churchName, RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
}
else{
getAuto().getToast(getContext(),RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + " unable to retrieve Church",RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
}
这不会在我的片段中返回任何数据
还有一件事,这是我用来填充父活动中BottomNavigationView的代码
aBar = getSupportActionBar();
aBar.setTitle("Rhema Social");
loadFragment(new RhemaHiveMessageFragment());
bottomNavigationView = findViewById(R.id.bottom_nav);
layoutParams = (CoordinatorLayout.LayoutParams) bottomNavigationView.getLayoutParams();
layoutParams.setBehavior(new RhemaHiveBottomNavBehaviorClass());
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
try {
switch (item.getItemId()) {
case R.id.rhem_user_messaging:
aBar.setTitle(getString(R.string.message));
loadFragment(new RhemaHiveMessageFragment());
return true;
case R.id.rhem_user_forum:
aBar.setTitle(getString(R.string.forums_lab));
loadFragment(new RhemaHiveGroupMessageFragment());
return true;
case R.id.rhem_user_starred:
aBar.setTitle(getString(R.string.starred_chats));
loadFragment(new RhemaHiveStarredChatsFragment());
return true;
case R.id.rhem_user_add_friend:
aBar.setTitle(getString(R.string.find_hivers));
loadFragment(new RhemaHiveFindHiverFragment());
return true;
}
}
catch(NullPointerException np){
getAuto().getToast(c,RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + np.getLocalizedMessage(),RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
}
return false;
}
});
}catch(NullPointerException np){
getAuto().getToast(c, RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + np.getLocalizedMessage(),RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
}
}
private void loadFragment(Fragment fragment){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.rhema_mess_cont,fragment);
transaction.addToBackStack(null);
transaction.commit();
}
您总是在onNavigationItemSelected中创建新的Fragment,并且不向Fragment中放入任何数据。像这样更改代码。
switch (item.getItemId()) {
case R.id.rhem_user_messaging:
aBar.setTitle(getString(R.string.message));
messageBundle = new Bundle();
rhemFrag = new RhemaHiveMessageFragment();
messageBundle.putString("user_church", churchName);
rhemFrag.setArguments(messageBundle);
loadFragment(rhemFrag);
return true;
case R.id.rhem_user_forum:
aBar.setTitle(getString(R.string.forums_lab));
loadFragment(new RhemaHiveGroupMessageFragment());
return true;
case R.id.rhem_user_starred:
aBar.setTitle(getString(R.string.starred_chats));
loadFragment(new RhemaHiveStarredChatsFragment());
return true;
case R.id.rhem_user_add_friend:
aBar.setTitle(getString(R.string.find_hivers));
loadFragment(new RhemaHiveFindHiverFragment());
return true;
或者,如果您已经在Activity的onCreate方法中创建了RhemaHiveMessageFragment,只需使用此片段,而不是像这样重新创建它。
switch (item.getItemId()) {
case R.id.rhem_user_messaging:
aBar.setTitle(getString(R.string.message));
loadFragment(rhemFrag);
return true;
case R.id.rhem_user_forum:
// ...rest of the code
您应该传递在onCreate中创建的RhemaHiveMessageFragment的新实例。
switch (item.getItemId()) {
case R.id.rhem_user_messaging:
aBar.setTitle(getString(R.string.message));
loadFragment(rhemFrag);
...
}
当您为底部导航设置片段时,您将为该片段创建新对象。所以,这并不是在其中添加论证者。因此,请使用您在onCreateView 中创建的RhemaHiveMessageFragment片段变量
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
try {
switch (item.getItemId()) {
case R.id.rhem_user_messaging:
aBar.setTitle(getString(R.string.message));
loadFragment(rhemFrag);
return true;
// other code will same
}
}
catch(NullPointerException np){
getAuto().getToast(c,RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + np.getLocalizedMessage(),RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
}
return false;
}
});
}