Reputation: 28379
I'm trying to find something analogous to a FragmentManager.peek() method.
The use case is that I have a need to send screen focus to the currently loaded Fragment. However, I'm finding myself having to keep manual track of what fragment is currently being displayed so that I can tell it to take focus.
Specifically the situation is that I have a menu which has a number of buttons. When a user clicks a button, I need to transfer focus from the button to a specific View within the currently loaded Fragment - but I have no way to know what Fragment is currently loaded, it may be the initial Fragment associated with the menu item, or it may be a different Fragment altogether that the user arrived at having dug deeper into the original Fragment... so my logic has to be
pseudo...
if(currentFragment == navButton.myFragmentType)
currentFragment.sendFocusToYourFirstRelevantElement()
else
navButton.loadMyFragmentType()
My problem is that I don't know what the currently displayed Fragment is, unless I keep track manually, but that's *really ugly.
Hoping there's a more native solution.
Upvotes: 0
Views: 66
Reputation: 151
You have to track Fragments manually.
Think about how Fragments work - they're designed to be reusable and, notably, there can be more than one on screen at a time. Because of this, it's not as simple as just querying the FragmentManager for the currently displayed Fragment, because it has no knowledge of which currently displayed Fragment you want.
The logic that that knowledge would require is not something that can be put in the FragmentManager because it's very specific to your app.
For more information: What Fragments am I hosting and displaying?
Upvotes: 1