onClickListener doesn't always fire in fragment

132 Views Asked by At

A problem that is driving me mad for some time (and it's blocking publishing to Amazon app store, since the app had been suppressed but now I am stuck with this bug and they won't republish it...).

I have a layout with a list on the left and a grid on the right (shown in the screenshot). Clicking items of the list on the left (categories) calls an api, returns some stuff (movies) and displays them in the right section. Then when you click on a movie in the right fragment, something else should happen.

It has been working for some time, no user had complained, AND it DOES work on certain devices... for example it works on the amazon Fire version that runs android 7.1 but it doesn't work on their android 5.1 nor on the android 9 versions... [EDITED 16/12/2020: see the edit at the bottom of the question... by changing libraries - downgrading back to older versions - it now appears to only happen on the Android 9-based Fire devices, and the older ones - both 5 and 7 - do work]

On the problematic devices this is the strange behaviour that happens: if you select one of the first three items in the list, in the grid on the right my onClickListener don't fire. Starting from the 4th item onwards, the onClickListener fires and my code executes.

This is only happening when using a remote/dpad.... touch events work also on the first three... but the problem happens on certain models of Amazon Fire TV and other Android TV's that don't have touch and have to use the remote (on one emulator I can reproduce it, on the other one I can't... even if both are running android 10...)

The right piece is made with fragments - it's always the same fragment, always the same api call (just a different category as parameter to the api call), always the same type of stuff that returns and needs to be displayed, passing thru the same adapter, holder, etc etc...

I am lost as to where to look into. I even tried downgrading libraries, compiler, etc... I can share pieces of the code (altough I have to do a lot of cleanup and it would still be quite a bit because it's a complex layout with many pieces... but it's very similar to this set onClickListener in getview() with ViewHolder class . I also checked this https://scottweber.com/2013/04/30/adding-click-listeners-to-views-in-adapters/ and I get the exact same behaviour...) but if the code was really broken, it would NEVER work... but the fact that it 'generally works' but not for the first three items in the list baffles me... as I already wrote: this would be the same fragment, same code... very weird. That's why I suspect a platform/library/compiler issue.. ...but I am lost as to where to look at...

enter image description here

EDITED / UPDATED 16/12/2020: After I submitted a version where I downgraded a LOT of libraries AND gradle as well (and no code change on my end), then I was able to get it approved on the Amazon store again but with limited devices compatibility. Basically they say it works on the oldest devices and breaks only on (some of) the newer ones... interesting. I still havent' found the culprit, but at least there is a sort of direction as to where to look at...

1

There are 1 best solutions below

0
On

It appears that if I set

android:descendantFocusability="afterDescendants"

on the GridView, then it works more reliably.

I am still doing more regression testing on old and new versions.... and I have no idea why the focus stealing happens only for some instances of that fragment and not for other ones...

Useful other threads I looked at and helped me in the right direction (and I had to buy a new Fire TV Stick of the new generation to be able to reproduce and debug it):