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

Nir Golan
  • 270
  • 1
  • 3
  • 15
  • We can't see all of your code, but where do you change the values of 'userDataService.name' and 'userDataService.email'. Because from here, it seems you receive the broadcast fine, and update the UI with the same values all the time. – NSimon Jan 02 '18 at 14:39
  • I change the userDataService inside the createUserActivity. When the db reply that the user created OK its populate the userDataService. And as I said before when I log the parameters in the onRecive its OK. – Nir Golan Jan 02 '18 at 15:44

2 Answers2

1

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

We can't see your entire code, but maybe you're not on the UI-Thread?

you can update the UI only from the main thread, you can use logs or debug to see on which thread you are to test that theory, or simply wrap your code inside a runOnUiThread (which will require activity context)

also, here's an interesting read on it if you feel like digging: Understanding Activity.runOnUiThread

JozeRi
  • 3,219
  • 6
  • 27
  • 45
  • just checked with Looper. the onRecieve runs on the main thread. – Nir Golan Jan 02 '18 at 12:59
  • well, i dont really understand why so if any one can explain i will be greatfull but i have added these lines in the main activity onCreate: userNameNavHeader.text = "name" userEmailNavHeader.text = "email" userImageNavHeader.setImageResource(R.mipmap.ic_launcher) and now the update goes well – Nir Golan Jan 02 '18 at 13:13