0

I have a static Handler in my MainAcitivty. I have "handleMessage" as well but am not able to access Objects like TextView which belong to MainActivity. Is WeakReference the way to go here in order to access these objects from within the handleMessage method?

edit: I need a static handler because that is the handler which is used by another thread to receive messages from that thread. So in other words: MainActivity shall display information it got from another thread.

stowoda
  • 103
  • 4
  • Post more details. Why you need static handler? Can't you use a handler as a field within the activity? Because if you have only one activity running at the time then you can use WeakReference (which is considered a bad practice of course but nonetheless). In case of two or more activities running at the same time you will have a mess. – GV_FiQst Sep 26 '19 at 19:47
  • you can use ```Handler handler = new Handler(Looper.getMainLooper());``` inside another thread if you want to post some code to the main thread – GV_FiQst Sep 26 '19 at 20:21
  • @GV_FiQst Yeah I already have that handler inside my worker thread. In my MainActivity I have handleMessage() within `public static Handler MainHandler = new Handler()`. But inside that handler, that handleMessage I am not able to access UI Objects. "..can not be referenced from static context.." – stowoda Sep 27 '19 at 14:40

0 Answers0