简体   繁体   English

Android片段回栈

[英]Android fragment backstack

I have shopping basket fragment in my app. 我的应用程序中有购物篮片段。 I want to show only one instance of that fragment. 我只想显示该片段的一个实例。 For example: 例如:

[1] [2] [my basket fragment] [3] [4] 

Then I need to display busket fragment again and replace it onto the top of the BackStack . 然后,我需要再次显示busket片段,并将其替换到BackStack的顶部。 The BackStack must become like this: BackStack必须变成这样:

[1] [2] [3] [4] [my basket fragment]

How is it possible? 这怎么可能? Because remove fragment by tag leaves empty view without content. 因为按标签删除片段会留下没有内容的空白视图。

If I understand you right, you are not wanting to have the basket fragment in your backstack. 如果我理解正确,那么您就不想在您的后备箱中放进篮子碎片。 That way, if the user presses back it wont show. 这样,如果用户按回它就不会显示。 To do that, don't add it to the backstack. 为此,请不要将其添加到堆栈中。

BasketFragment yourInstanceOfTheBasketFragment = new BasketFragment();

// to show the fragment
getFragmentManager()
          .beginTransaction()
          .replace(R.id.container, yourInstanceOfTheBasketFragment)
          .commit();

// to remove the current fragment, and show next in backstack.
getFragmentManager()
          .popBackStack();

Also, note that removeFragment will only do that. 另外,请注意, removeFragment只会这样做。 It wont add anything. 它不会添加任何东西。 So if you are wanting to remove a fragment and show what is below it in the stack, call getFragmentManager.popBackStack(); 因此,如果要删除片段并显示堆栈中的片段,请调用getFragmentManager.popBackStack();

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

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