问题
In Android P
, application on start not working as expected with throwing warnings :
W/ViewRootImpl:
Dropping event due to no window focus: MotionEvent { action=ACTION_DOWN, KeyCode = DPAD_DOWN}
and
W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_UP, KeyCode = DPAD_DOWN} and its not allowing any action to performed.
Check Image for detailed warning.
Note: Same Application is working fine in Android O
, but in Android P
, sometimes the focus is missing in PopupMenu
items and not getting back the focus until reboot the system. After using
dispatchKeyEvent
and setting dynamically focus to the view as v.requestFocus();
the application started working little better but still the warning is observed sometimes with feel like system hanged for a user. Check below code snippet for details:
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
LogUtils.d(TAG, String.valueOf(event.getKeyCode()));
this.getWindow().getDecorView().setFocusable(true);
return super.dispatchKeyEvent(event);
}
My Question is, how to set the missing focus dynamically to the PopupMenu
and RecyclerView
or how to overcome this in application running in Android P.
Please suggest, I got same type of question in stackOverflow but not a proper solution, so posting the question and waiting for all type of suggestion and answer. Thanks in advance.
回答1:
I have the same problem on android p when show PopupWindow
and i solved it but by View.post()
like this:
getView().post(new Runnable()
{
@Override
public void run()
{
// show popup window
}
});
hope can help you
回答2:
In my case similar issue was caused by using onKeyDown() and KeyEvent.ACTION_DOWN everywhere, including when showing a dialog on a key press. Using onKeyUp() and KeyEvent.ACTION_UP to show dialogs and activities resolved it for me.
来源:https://stackoverflow.com/questions/54702989/in-android-p-dropping-event-due-to-no-window-focus-keyevent