从后面堆栈中删除片段 [英] Removing a Fragment from the back stack

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

问题描述

我有活性的3个片段,当平板电脑在纵向举行。不过我只有2这些片段的景观时。我遇到的问题是会从纵向到横向活动正在创造第三个片段时。余接收和不能被创建的错误,因为这片段。

这该片段正在创建,因为它是在后面堆栈我曾出

我曾尝试通过删除片段中的onDestroy方法

  FragmentTransaction F = fragmentManager.beginTransaction();
f.remove(MF);
f.commit();
 

但是,我得到一个错误,说我不能的onSaveInstanceState

在使用此功能

什么是采取这一片段了回来堆的正确方法是什么?

更新

我也许应该补充一点,我有问题的片段是mapFragment从这个libary

<一个href="https://github.com/petedoyle/android-support-v4-googlemaps">https://github.com/petedoyle/android-support-v4-googlemaps

我用它的方式是像这样

  MF = MapFragment.newInstance(1,真正的);

        英尺= fragmentManager.beginTransaction();
        ft.replace(R.id.mapContainer,MF);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(图);
        ft.commit();
 

解决方案

您从 FragmentTransaction 添加到恢复状态,并使用 FragmentManager pop方法:

  FragmentManager经理= getActivity()getSupportFragmentManager()。
FragmentTransaction反式= manager.beginTransaction();
trans.remove(myFrag);
trans.commit();
manager.popBackStack();
 

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.

I have tried to remove the fragment in the onDestroy method by using

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

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?

Update

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();

解决方案

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();

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

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