It's been a few days and I've gotten no response on the questions below, so I 
suppose I'll assume there is no way to to accomplish getting the fragment 
associated with a BackStackEntry off the backstack without a specific name or 
ID for it. In my code, I have had to manage a separate true java.util.Stack in 
parallel with the backstack to properly manage things (which is complete 
redundancy of processing, but required due to the API). 

If anyone close to the Android API dev team is reading this, I would strongly 
suggest a change to the API here which would allow you to get the Fragment 
associated with the BackStackEntry popped off the stack. As a measure of 
sensible protocol, if a caller pushes A onto a stack, it doesn't make sense 
that they are handed back B on a pop operation, and worse, can't directly get 
A. If it is important to have B in the mix at all (which in this case would be 
BackStackEntry), then then the API needs to do one of the following: 

1. Push A, pop -> A (this is the best idea, if B is needed internally, fine, 
then have B wrap A while it is on the stack, and unwrap A from B when popped). 

2. Push A, pop -> B, but then B needs an explicit getA() method, not a 
getAById() or getAByName(), but just getA(). One note on B, if you are going to 
pop an object off the stack which is different than the object pushed onto the 
stack, it needs to have specific relevance to the caller, and direct access to 
the original object pushed. If B is not directly relevant to the caller, then 
popping off B essentially represents a propagation of internal implementation 
out of its black box. The point of reuse with fragments and when it comes to 
stacks in general is that you aren't concerned about asking for stack objects 
in specific manner, you are only interested in "what's on top". And in that 
regard, the backstack really ought to have a peek() or top() convenience method 
which doesn't require getting the backstack entry count minus one. 

I hope that contribution helps -- this is a question that has gotten some 
considerably mileage on other forums, so it would appear that a number of 
others have traveled the same terrain. 

Onward and upward, 

Brad 

On Oct 13, 2013, at 11:25 AM, Brad O'Hearne <[email protected]> wrote:

> 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. 



-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to