我尝试在单击每个片段底部导航项时重新创建活动,但是它不起作用。通知徽章消失的唯一方法是停止应用程序并再次将其重新打开
带有代码的帖子,这是我将徽章添加到底部导航栏的示例之一。
在进行了几项研究之后,谢谢
navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.Services:
selectedFragment = new services_fragment();
break;
case R.id.my_task_post_view:
selectedFragment = new my_posts_fragment();
break;
case R.id.Search:
selectedFragment = new search_list_fragment();
break;
case R.id.Message:
selectedFragment = new Message_fragment();
break;
case R.id.Account:
selectedFragment = new Account_fragment();
break;
}
onResume();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
});
@Override
protected void onResume() {
super.onResume();
onStart();
// check if there is any message notification
messageBadge();
// check if the account needs to have a notification badge\
accountBadge();
postBadge();
bidsBadge();
tasksBadge();
myCompletedTaskBadge();
}
private void accountBadge() {
notification_counter.child(current_user_id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
if (dataSnapshot.child("status").getValue().toString().equalsIgnoreCase("false")) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) navigationView.getChildAt(0);
final BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(4);
notification_badge_account = LayoutInflater.from(Services.this).inflate(R.layout.view_notification_badge_account, menuView, false);
// TEXTVIEW
if (notification_badge_account.getParent() != null) {
((ViewGroup) notification_badge_account.getParent()).removeView(notification_badge_account); // <- fix
}
itemView.addView(notification_badge_account); // <========== ERROR IN THIS LINE DURING 2ND RUN
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
我真正想要的是,如果用户单击底部导航栏中的不同项目,则通知条件应消失,而无需停止应用程序并再次运行它。它认为由所有片段组成的主要活动没有得到刷新,因此正面临这种行为