简体   繁体   English

如何获取对最后一个backstack弹出片段的引用?

[英]How to get a reference to the last backstack popped fragment?

I add, show and hide fragments. 我添加,显示和隐藏片段。 Each time I add/show a fragment, I hide the previous fragment and add the transaction to the backstack. 每次添加/显示片段时,我都会隐藏前一个片段并将事务添加到backstack。

When a user presses the back button, a fragment is popped and I would like to have a reference to it . 当用户按下后退按钮时,会弹出一个片段, 我想引用它

Why do I need a reference? 为什么我需要参考? So I could hide it when the user continues to the next fragment. 所以我可以在用户继续下一个片段时隐藏它。

So, how do I get a reference to a popped fragment? 那么,我如何获得对弹出片段的引用?

EDIT-25-04-2013: 编辑-25-04-2013:

Here's code to explain how to add a new fragment, while hiding the previous one. 这里是解释如何添加新片段的代码,同时隐藏前一片段。 The question is how to get a reference to the last fragment after it is popped from the backstack (using the back button)? 问题是如何在从Backstack弹出后使用后退按钮获取对最后一个片段的引用?

FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.hide(lastFragment);
fragmentTransaction.add(newFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
lastFragment = newFragment;

I use generated tags for each fragment, save the tags in a stack and persist the stack. 我为每个片段使用生成的标签,将标签保存在堆栈中并保持堆栈。 This way I get hold of every fragment out there - last one in specific. 通过这种方式,我抓住了每个片段 - 最后一个特定的片段。 See the code here . 请参阅此处的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM