Drag and Drop crash in Android 9 Pie

1409 views android
6

When I try my app on Android 9 Pie, I cannot get the Drag & Drop functionality to work the same as in previous version. Basically what happens is that calling event.getLocalState() returns null. This happens ONLY on Android 9. Snippet:

protected final class DragListener implements View.OnDragListener {

    @Override
    public boolean onDrag(View v, DragEvent event) {

        // Here I get null
        final NumberButton draggedButton = (NumberButton) event.getLocalState();
    }
}

I initialize the drag in OnTouchListener:

protected class MyTouchListener implements View.OnTouchListener {

    private float pressedY;
    private float pressedX;

    @Override
    public boolean onTouch(final View view, MotionEvent motionEvent) {

        NumberButton draggedButton = (NumberButton) view;

        if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
            // DOING SOMETHING HERE
            return true;
        } else if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
            if (getMoveDistance(motionEvent, pressedX, pressedY) >= 10) {
                draggedButton.markSelected();
                View.DragShadowBuilder shadowBuilder = new Utils.ButtonDragShadowBuilder(view, motionEvent.getX(), motionEvent.getY());
                try {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        view.startDragAndDrop(null, shadowBuilder, draggedButton, 0);
                    } else {
                        view.startDrag(null, shadowBuilder, draggedButton, 0);
                    }
                } catch (IllegalStateException e) {
                    Log.e("Number button", e.getLocalizedMessage());
                    return false;
                }
            }
        }
    }
}

By looking at the source code, I see that startDragAndDrop has different implementation in Android 8 and 9.

First of all, any clues why this is happening?

Second and more importantly, how to fix/go around this issue? Drag & Drop is the key part of my app and I would like it to work on the newest Android.

Thanks for help

answered question

Do you have better luck if you use a simpler data type for the local state (e.g., an int)?

You mean e.g.: view.startDragAndDrop(null, shadowBuilder, 5, 0); ? I tried and still getting null upon int state = (int) event.getLocalState();

Yeah, that's what I meant. Phooey.

1 Answer

0

Looks like this was reported as a bug in July. At that point, it was probably too late to address in the shipping Android 9.0, and it's unlikely to get fixed via a security patch.

In terms of a workaround, assuming there is only one drag at a time, you should be able to keep track of what the current drag is dragging somewhere else (e.g., field in the activity/fragment/viewmodel).

posted this

Have an answer?

JD

Please login first before posting an answer.