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