Drag and Drop not working between 2 activities on some devices

谁说胖子不能爱 提交于 2019-12-21 12:57:38

问题


I use the following code to drag a view from an activity to another activity within my application. Knowing that the second activity (That receives drop event) is not created / alive when the drag starts.

It works good on

Samsung Note 3 Android 5 API 21, Samsung Note 4 Android 6.0.1 API 23

but not working on

ASUS ZenPad 8.0 Android 5.1.1 API 22, Le Max 2 Android 6.0 API 23

Your thoughts are appreciated.

Starting Drag operation:

public boolean onItemLongClick(AdapterView<?> aParent, View aView, int aPos, long aID) {

    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(aView);

    Intent intent = prepDNDIntent();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        aView.startDragAndDrop(ClipData.newIntent(DRAG_N_DROP_DESCRIPTION, intent), shadowBuilder, null, 0);
    } else {
        aView.startDrag(ClipData.newIntent(DRAG_N_DROP_DESCRIPTION, intent), shadowBuilder, null, 0);
    }


    startMainActivity();

    finishThisActivity();

    return true;
}

Receiving Drop Operation

public boolean validDragNDropOperation(View aView, DragEvent aEvent){
    boolean status = false;

    ClipDescription clipDescription = aEvent.getClipDescription();
    if (clipDescription != null && clipDescription.getLabel().toString().equalsIgnoreCase(DRAG_N_DROP_DESCRIPTION)) {
        status = true;
    }

    return status;
}


public boolean onDrag(View aView, DragEvent aEvent) {

    switch (aEvent.getAction()) {
          case DragEvent.ACTION_DRAG_STARTED:

               This is not called on some devices

               return validDragNDropOperation(aView, aEvent, false);

          case DragEvent.ACTION_DROP:
                 break;
    }

    return true;
}

Knowing that I set both Activities to android:launchMode="standard" and to "singleTask" but I ended with the same result.

EDIT

During testing, I used an AlertDialog that holds the views being dragged (Instead of using activity), and I faced the same problem, when dragging the view from the AlertDialog to the Activity (That owns the AlertDialog), ACTION_DRAG_STARTED not called.

来源:https://stackoverflow.com/questions/52711596/drag-and-drop-not-working-between-2-activities-on-some-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!