0

When there are multiple Fragments on the screen, how do I tell when the user interacts in ANY way with a specific one of the Fragments? Perhaps they tap a button, scroll a list, tab through fields, anything.

I had thought perhaps I would get an onClick/onTouch/onFocusChanged listener on the Fragment/FrameLayout containing the Fragment/root Layout of the Fragment UI, but they don't fire.

I'll try setting a variety of manual triggers, like "if the user clicks this button or scrolls this view or this field gains focus, then the user has interacted with this Fragment" but obviously that's more complex and highly prone to inaccurate behavior.

Not sure why I need this? Here's the use case:

Think of Gmail on a tablet: in landscape, the inbox is shown on the left and the first message on the right. If I tap a different message, it is displayed on the right. If I rotate to portrait, it will show the message I had last selected.

If I start in portrait, it will show the inbox.

Now here's the real trick: let's say I start in landscape, showing inbox on the left, the first message on the right. If I immediately rotate the device to portrait. But if I interact with the pane on the right (say, scrolling the email message) and then rotate the device, it will show that first message. Gmail guesses whether I was more interested in the inbox or the email and shows that.

My challenge is doing something similar in an app. When I'm rotating from landscape (showing two panes) to portrait (showing one pane) I'm not sure whether to show the list view or the default detail view. If I could tell if the user interacted with the detail view in some way (via keyboard or touchscreen) then I know to show that.

Chad Schultz
  • 7,770
  • 6
  • 57
  • 96
  • Have you tried this: http://stackoverflow.com/questions/21882251/how-to-handle-touch-events-on-a-fragment ? If your fragment doesn't get the event that means some other view intercepts it. – Gennadii Saprykin Sep 30 '16 at 02:59
  • Yes, I have tried an onTouchListener on the root layout. It doesn't fire. – Chad Schultz Sep 30 '16 at 15:24

1 Answers1

0

In the end, I could not find any good, clean way to work this.

Instead, for every individual way the user could interact with the Fragment--touching a ListView, clicking a Button and so on--I would fire off a message to the Activity (using a Listener interface the Activity implements, of course).

Then the Activity tracks whether or not the user has interacted with the default Fragment, making sure to save and restore that value.

It's messy, but it's all I could come up with. :/

Chad Schultz
  • 7,770
  • 6
  • 57
  • 96