I have noticed something interesting with how View.post(Runnable r)
works as opposed to using a Handler to post back to the UIThread, and
I'm wondering if all of you out there can perhaps pick up on something
that I am not seeing (a problem in my code I believe). Perhaps the
multiple threads are causing some issues with how the post is being
processed but I will describe the high level flow of the code below.
I have a block of code called from a View subclass, on the main
thread, that establishes a background thread so as not to block the UI
thread, and then from within that thread calls the post() method with
another anonymous Runnable class to update the UI:
{
// do work in background thread so the UI thread isn't
blocked.
new Thread()
{
public void run()
{
// do work to retrieve info to initialize view
here
post( new Runnable()
{
public void run()
{
// perform animation to fade
initialized view into a view group
}
});
}
}.start();
}
When the view that contains this code is added as a footer in a
ListView that goes off the bottom of the screen (so the view is not
visible), the Runnable object passed to post() does not seem to get
its run() called. If I put a break in the code before the post,
scroll the ListView so that the footer is visible onscreen and then
continue, the Runnable object's run() method does get called.
If I use a Handler() to keep a reference to the UI thread's handler,
then the code executes without fail, whether or not the view is
showing on screen:
{
// get Handler referring to UI Thread.
Handler uiThreadHandler = new Handler();
// do work in background thread so the UI thread isn't
blocked.
new Thread()
{
public void run()
{
// do work to retrieve info to initialize view
here
uiThreadHandler.post( new Runnable()
{
public void run()
{
// perform animation to fade
initialized view into a view group
}
});
}
}.start();
}
Has anyone else seen weirdness like this or have ideas as to where the
first block of code has gone wrong?
Thanks much in advance,
Mike
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---