0

Situation *************: I'm looking some intel about the onKey override method by fragment. I have single activity several fragment structure. Only one fragment visible at time. I show a new fragment with a transaction add fragment ( because I don't want recreate the previous fragment when back so I don't use replace) + setPrimaryFragment each time for the top fragement. I go back with transaction remove fragment + setPrimaryFragment to update with the top fragment. On my fragment, I override onKey method.

Now my problem ************* Sometimes the onKey (back button) is not received by the top fragment (visible and primary) but received by fragment not visible and not primary.

case 1: With 2 fragments, open and back, works fine.

with 3 fragments, open fr1, open fr2, open fr3 and back to fr2(remove and destroy fr3). So now if I push back again, onKey is not call on the second fragement fr2 (visible and primary) but by the first fragment fr1 (not visible and not primary).

Is there someone can tell me why? How the onKey decide which fragment to call ?

For now, I solve this by checking inside onKey if the fragement is primary, if not I dispatch myself the call to the primary. But I still want to understand why...

Thanks.

1 Answer 1

0

Not sure if answering to my request is usefull but I finnaly found where come this behavior. Even if the fragment is the top visible fragment, it deosn't mean it has the focus. only the fragment who has the focus got onKey method fired. Poping or replace doesn't transfer the focus, you need do it yourself with the method request focus.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.