Stack Overflow Asked by Ricky P on January 23, 2021
Steps: If there are 4 RecyclerView layouts in the notification fragment, the Notification Badge will read a count of 4. When I click the Notification navigation btn, the count clears. If I input a new recyclerView the counter reads 5, instead of resetting to 1.
I want the notification badge to only count the new Notifications that come in.
Also once the app closes and reopens the notification badge, re-counts the old Notifications and the new ones together.
Once the app loads
Clicking notification Btn
package supgro.com.Controller.Service
class MainActivity (): AppCompatActivity() {
private lateinit var firebaseUser: FirebaseUser
var post: MutableList<Post>? = null
var noti: MutableList<Notification>? = null
/* This selectedFragment was used to test when the buttons were clicked they would go to that fragment/activity
selectedFragment = HomeFragment()
internal var selectedFragment: Fragment? = null*/
private val count: Int = 1
private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.nav_home -> {
//passing in moveToFragment function
moveToFragment(HomeFragment())
return@OnNavigationItemSelectedListener true
}
R.id.nav_search -> {
moveToFragment(SearchFragment())
return@OnNavigationItemSelectedListener true
}
R.id.nav_add -> {
moveToFragment(PostSelectionFragment())
//item.isChecked = false
//startActivity(Intent(this@MainActivity, AddPostActivity::class.java))
return@OnNavigationItemSelectedListener true
}
R.id.nav_notifications -> {
if (moveToFragment( NotificationsFragment()) != null){
badgeClear(R.id.nav_notifications).apply {
isDestroyed
// new_notifications_icon.visibility = View.VISIBLE
}
}
return@OnNavigationItemSelectedListener true
}
R.id.nav_profile -> {
moveToFragment(ProfileFragment())
return@OnNavigationItemSelectedListener true
/* if (item.isEnabled){
item.isVisible = false
}*/
}
}
/* if (selectedFragment != null){
supportFragmentManager.beginTransaction().replace(R.id.fragment_container, selectedFragment!!).commit()
}*/
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
firebaseUser = FirebaseAuth.getInstance().currentUser!!
//bottom_nav_view is the name of the bottom navigation bar
val navView: BottomNavigationView = findViewById(R.id.bottom_nav_view)
navView.setOnNavigationItemSelectedListener (onNavigationItemSelectedListener)
//this selects the first fragment activity to be loaded
navView.getOrCreateBadge(R.id.nav_notifications).apply{
val notiRef = FirebaseDatabase.getInstance().reference.child("Notifications")
notiRef.addValueEventListener(object: ValueEventListener {
override fun onDataChange(p0: DataSnapshot) {
if (p0.exists()) {
number = p0.childrenCount.toInt()
isVisible = true
// noti!!.clear()
}/*else if (p0.childrenCount <= 5){
number = p0.childrenCount.toInt()
}*/
}
override fun onCancelled(p0: DatabaseError) {
TODO("Not yet implemented")
}
})
}
// navView.removeBadge(R.id.nav_notifications)
moveToFragment(HomeFragment())
}
private fun moveToFragment(fragment: Fragment){
val fragmentTrans = supportFragmentManager.beginTransaction()
fragmentTrans.replace(R.id.fragment_container, fragment)
fragmentTrans.commit()
}
private fun badgeClear(id: Int){
val navView: BottomNavigationView = findViewById(R.id.bottom_nav_view)
val badgeDrawable = navView.getOrCreateBadge(id)
if (badgeDrawable != null){
badgeDrawable.isVisible = false
badgeDrawable.clearNumber()
}
}
}
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP