Local Broadcast not updating UI


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

and in the main activity the reciever:

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”,

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"
and now the update goes well, can anyone help me understand why?



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



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?) {

            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)



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.