从后栈中移除一个 Fragment [英] Removing a Fragment from the back stack

查看:36
本文介绍了从后栈中移除一个 Fragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当平板电脑竖放时,我在活动中有 3 个片段.但是,在风景中我只有 2 个这样的片段.我遇到的问题是当从纵向到横向时,活动正在创建第三个片段.我收到错误消息,因为无法创建此片段.

I have a 3 fragments in an activity when the a tablet is held in portrait. However I only have 2 of these fragments when in landscape. The problem I am having is when going from portrait to landscape the activity is creating the 3rd fragment. I receive and error as this fragment cannot be created.

我已经确定这个片段正在被创建,因为它在后堆栈中.

I have worked out that this fragment is being created because it is in the back stack.

我尝试使用

FragmentTransaction f = fragmentManager.beginTransaction();
f.remove(mf);
f.commit();

但是我收到一个错误,说在 onSaveInstanceState 之后我不能使用这个函数

However the I get an error saying that I cannot use this function after the onSaveInstanceState

从返回栈中取出这个片段的正确方法是什么?

What would be the correct way of taking this fragment out of the back stack?

我可能应该补充一点,我遇到问题的片段是来自这个库的 mapFragment

I should probably add that the fragment I am having problems with is a mapFragment from this libary

https://github.com/petedoyle/android-support-v4-googlemaps

我使用它的方式是这样的

The way I use it is like so

mf = MapFragment.newInstance(1, true);

ft = fragmentManager.beginTransaction();
ft.replace(R.id.mapContainer, mf);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack("map");
ft.commit();

推荐答案

您从 FragmentTransaction 添加到后台状态,并使用 FragmentManager 弹出方法从后台移除:

You add to the back state from the FragmentTransaction and remove from the backstack using FragmentManager pop methods:

FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove(myFrag);
trans.commit();
manager.popBackStack();

这篇关于从后栈中移除一个 Fragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆