Local Broadcast not updating UI

#1

i have this code in my register user activity:
val userDataChange = Intent(BROADCAST_USER_DATA_CHANGE)
userDataChange.putExtra(“aaa”, “aaaaaaaa”)
userDataChange.putExtra(“bbb”, 123)
LocalBroadcastManager.getInstance(this).sendBroadcast(userDataChange)

and in the main activity the reciever:
LocalBroadcastManager.getInstance(this).registerReceiver(userDataChangeReceiver,
IntentFilter(BROADCAST_USER_DATA_CHANGE))

and the userDatachangeReceiver function:
private val userDataChangeReceiver = object: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.d(“aaa”, AuthService.isLoggedIn.toString())
userNameNavHeader.text = "BBBBBBBBBBB"
if (AuthService.isLoggedIn == true) {
userNameNavHeader.text = userDataService.name
userEmailNavHeader.text = userDataService.email
val resourceId = resources.getIdentifier(userDataService.avatarName, “drawable”,
packageName)
userImageNavHeader.setImageResource(resourceId)

userImageNavHeader.setBackgroundColor(userDataService.returnAvatarColor(userDataService.avatarColor))
loginButtonNavHeader.text = “Logout”
}
}
}

the create user activity updates the userDataService(singelton) just fine,
when i log it i get the right parameters. the AuthService.isLoggedIn returns true but the drawer UI does not gets updated i have tryed to put it int the onStart, onResume unregister on onPause but no luck, also when i try to access the bundle inside the intent i get null. i put it there just to see if its really getting thrugh any ideas?

after i added these lines in the onCreate of MainActivity:
userNameNavHeader.text = "name"
userEmailNavHeader.text = "email"
userImageNavHeader.setImageResource(R.mipmap.ic_launcher)
and now the update goes well, can anyone help me understand why?

0 Likes

#2

This seems to be a question on Android programming in general; please ask it on StackOverflow instead.

0 Likes

#3

You first need to obtain the reference to the nav_header_main layout first to be able to update the text-View and image-view in the nav_header_main. Instead of using the id of the elements directly you should use nav_header reference then id like this

private val userDataChangeReceiver = object: BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {

        if(AuthService.isLoggedIn){
            Log.d("qwe", UserDataService.name)
            Log.d("qwe", UserDataService.email)
            Log.d("qwe", UserDataService.avatarName)
            Log.d("qwe", "Made it inside the broadcast bro")

            Log.d("qwe", "Wrap")


            nav_drawer_header_include.userEmailNavHeader.text = UserDataService.email
            nav_drawer_header_include.userNameNavHeader.text = UserDataService.name
            userEmailNavHeader.text = UserDataService.email
            val resourseid= resources.getIdentifier(UserDataService.avatarName,"drawable",packageName)
            nav_drawer_header_include.userImageNavHeader.setImageResource(resourseid)
            nav_drawer_header_include.loginBtnNavHeader.text="Logout"



        }

    }
}

We should use Log most often, it helped me to get to the problem directly instead of wasting my time on perfectly fine working things.

I too stumbled on this problem while doing the Android Kotlin course but after searching I found this is faced by many people they asked question on different platforms but no answer was provided. But after searching same terms “UI elements not updating android” found some stack overflow answers in java https://stackoverflow.com/questions/34973456/how-to-change-text-of-a-textview-in-navigation-drawer-header

They all explained to first get reference then we can be able to update the View. Then I tried and fortunately it worked perfectly fine.

PS: This is my first answer please don’t mind the mistakes, I apologize.

0 Likes