3

I've got to be missing something completely stupid on this one since updating a TextView should be an easy task. I have an Activity class that is an observer of another that receives messages. In my Activity's onCreate I do the following and it works fine.

theStatus = (TextView) findViewById(R.id.theStatus);
theStatus.setText("waiting");

Later when the message activity receives a new message it hands it off to it's observer (my Activity)

public void statusUpdate( GenericMessage aMessage )
{
    String statusText = aMessage.getDetails();
    theStatus.setText(statusText);

    theSessionStatus.invalidate();  // I've tried with and without this call
}

However the screen doesn't update. I must be overlooking something...

Dave C
  • 7,729
  • 4
  • 49
  • 65
bursk
  • 1,647
  • 7
  • 25
  • 39
  • Are you sure `statusUpdate()` is even being called? – CommonsWare Sep 03 '10 at 20:27
  • I'm sure that statusUpdate is being called. I'm using the debugger and catching breakpoints I've set there. One detail I'm now realizing may be affecting me is that this Activity is not the main activity for the app. Short story is the main app starts up and waits for the user to click a button to start. Once started it starts up the message thread and based on a message from it decides what Activity to display to the user. It starts this Activity by calling startActivityForResult and the Activity then registers itself as an observer of the message thread. – bursk Sep 03 '10 at 20:34
  • You should make sure that the activity which is in progress handles the statusUpdate() call. – Karan Sep 04 '10 at 08:24
  • I have made sure that statuUpdate() is being called. As I stated in my previous comment I am stepping in with the debugger. I can step over the setText call and see the text in the variable's properties, but the screen does not update. – bursk Sep 04 '10 at 11:32
  • I guess what Karan means is that `statusUpdate` should be called on UI thread. Have you tried updating the `TextView` from your main activity? – Asahi Sep 04 '10 at 12:50
  • I think that's on the right track. I am now seeing an exception being kicked out when I call setText "Only the original thread that created a view hierarchy can touch its views". I guess I'm confused. Can't an Activity kick off another using startActivityByResult and then let the new Activity's class handle its own updates? Activity A starts Activity B. I don't want A to have to know about the widgets on B's screen. That seems to break the whole idea of OO programming. I really appreciate the help. Very frustrating. – bursk Sep 04 '10 at 13:21

1 Answers1

0

It looks like I found a solution, although it feels like a hack. I got the idea from your comments above and from here.

I am now using a Handler to notify the Activity B that a message has come in that it is interested in. B creates a Handler in its onCreate:

  Handler statusHandler = new Handler();
  statusHandler.post(new Runnable() 
  {
      public void run()
      {
         statusUpdate();
      }
  });

I updated the message receiver class C (that B was trying to be an Observer of) to take in the Handle when B registers with it. Then in C, I call the following when the applicable message comes in:

theMsgObserverHandle.post(new Runnable()
{
   public void run()
   {
      Log.d(myName, "Calling my Observer statusUpdate");  

      theMsgObserver.statusUpdate();
   }
});

What I don't like is that now I can't just pass my message to B directly. (My message class is my own defined class not android.os.Message) Instead now in B I need to ask the instance of C (C is a Singleton) for the message. But at least I'm moving forward. If anyone has a better implementation I'd really appreciate the feedback.

Community
  • 1
  • 1
bursk
  • 1,647
  • 7
  • 25
  • 39
  • Odd, on further reading it seems like I should not have to call the first statusHandler.post that I have in my onCreate, but if I take it out, while statusUpdate is called and no longer throws an exception the setText doesn't work. – bursk Sep 04 '10 at 15:37