Sunday, October 13, 2013

[android-developers] Getting the top fragment in the backstack

Hello all,

I am trying to manage the pushing and popping of fragments on a fragment manager's backstack. I have discovered that while I can easily get the backstack's top BackStackEntry (suggestion: a top() method would be nice), I cannot very easily get the fragment associated with it.

I understand that the quick answer is going to be something like use the BackStackEntry's ID to find the fragment, as in:

Fragment topFragment = fragmentManager.findFragmentById(backStackEntry.getId());

or to use the BackStackEntry's tag to find the fragment, as in:

Fragment topFragment = fragmentManager.findFragmentByTag(backStackEntry.getName());

but I don't believe I can do either, and the reason is that there may be more than one fragment in the backstack of the same type. Imagine a fragment that is displaying details on a car, and displays with it a list of other suggested cars. Selecting one of those suggested cars will create another fragment of the same type, and push it onto the same backstack. This can happen any number of times.

The problem is that all car fragments would have the same ID, and managing separate names for all of them -- what a complete nuisance, and duplicated effort. The whole point behind fragments is reuse, and the whole point behind a stack data structure is dealing with the top item as it appears. So questions:

1. How can I get the fragment associated with the top item in the backstack by a means other than an ID or a name? (I am hoping that I'm not going to have to manually manage a fragment stack in parallel to the backstack -- surely there has to be a way to do this easily).

2. Any idea why when adding something to the backstack, you can't just get it back when you ask for it? At the API level, the caller isn't adding a BackStackEntry, they are directly pushing their fragment. If internally it needs to be wrapped in a BackStackEntry to provide additional metadata, fine, but why isn't there a getFragment() method on the BackStackEntry? If that entry is a direct result of the fragment being added, why all the loose-coupling and indirection? This is one instance where loose-coupling is not a good thing.

Your insight and answers are appreciated.

Thanks,

Brad

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate