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

6
  • Are you sure statusUpdate() is even being called? Sep 3, 2010 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 3, 2010 at 20:34
  • You should make sure that the activity which is in progress handles the statusUpdate() call.
    – Karan
    Sep 4, 2010 at 8: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 4, 2010 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 4, 2010 at 12:50

1 Answer 1

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.

1
  • 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 4, 2010 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.