Search code examples
androidandroid-mapviewhandler

Update the UI from background thread during touch event


I'm trying to add an overlay to a MapView from a background thread to optically mark the point the user is currently touching.

At the moment I try to achieve that using Timer/Handler. When the MotionEvent is ACTION_DOWN, I post a task, which adds the circle overlay to the mapView overlays after a certain delay.

Unfortunately, the UI is only updated after the user removes the finger from the screen (ACTION_UP).

Here's the sample code:

UI thread

if (event.getAction() == MotionEvent.ACTION_DOWN) {
    handler = new OverlayHandler(mapView, centerDrawer) // centerDrawer is an overlay drawing a circle
    cTask = new DrawTimer(handler); // Calls handler.sendMessage, telling the handler to add centerDraw to the overlays of mapView

    handler.postDelayed(cTast, delay);

} else {
    handler.removeCallbacks(cTask);
}

Timer (DrawTimer etc.)

public void run() {
    Message msg = new Message();
    msg.what = 1;
    handler.sendMessage(msg);
}

Handler

public void handleMessage(Message msg) {
    switch (msg.what) {
    case 1:
            mapView.getOverlays().add(centerDrawer);
    break;
        // more cases
    }
    super.handleMessage(msg);
}

As far as I know, this is the default way to interact with the UI from a background task.

Is this also valid for updates while the user is interacting with the screen? Do I have to explicitly tell the view to render new overlays?

EDIT:

The code should do the following: On touch, it should start to observe how long the user keeps touching. After an inital delay, the circle should be added to the view. After some more delay, another overlay is added and so forth.

I do that for each overlay in two steps: I add a Runnable to handler to be executed after a delay. The Runnable then sends the corresponding message to the handler, telling it, what to draw. I'm doing it like that, because...

  • I need to be able to cancel the timer (the post), if the user moves or releases its finger.
  • Some overlays should change, when the user keeps touching the screen. The postDelayed Runnable just keeps sending messages till canceled.

That's why I don't just use delayed messages.

Use cases

  • User touches screen -> user releases before delay passes (nothing should appear)

  • Touch -> delay passes -> circle -> release

  • Touch -> delay passes -> circle -> delay 2 passes -> circle 2 -> release


Solution

  • Found the answer in this question: Views Don't Update Until MapView is Touched

    mapView.invalidate() forces the view to redraw all its overlays. It might not be the most efficient way to implement that behavior but it works the way I want it to.

    Thanks to farble1670 for pointing at some problems with my prior implementation. Following his advice I changed it to use messages only (which are cancelable as well using removeMessage).

    The new code is even simpler:

    UI Thread

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        handler = new OverlayHandler(mapView, centerDrawer)
    
        handler.sendEmptyMessageDelayed(1, d);
    
    } else {
        handler.removeMessage(1);
    }
    

    Handler

    public void handleMessage(Message msg) {
        switch (msg.what) {
        case 1:
            mapView.getOverlays().add(centerDrawer);
        break;
            // more cases
        }
        mapView.invalidate(); // Forces redrawing of all overlays
        super.handleMessage(msg);
    }