FragmentTransaction更换不工作 [英] FragmentTransaction replace not working

查看:240
本文介绍了FragmentTransaction更换不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了计算器上的这个问题,发现这两种解决方案:

<一个href=\"http://stackoverflow.com/questions/5658675/replacing-a-fragment-with-another-fragment-inside-activity-group\">Replacing里面活动组结果另一个片段的片段
片段添加或更换不工作

但我的code似乎是正确的,你可以看看下面,在我的code中的评论是葡萄牙语(巴西):

这是我的课MenuItemReceiver(是的,我用一个BroadcastReceiver来显示我的片段),这已经工作来展示我的片段内容,但是当我试图取代它,旧的片段住宿和新才出现上面的老之一。

 公共无效的onReceive(上下文的背景下,意图意图){
        //片段世事不可强求carregado,埃斯特片段FOI repassado PELO SlideMenuAdapter鳌之三庵项目clicado
        序列化serializableExtra = intent.getSerializableExtra(CustomFragment.CUSTOM_FRAGMENT);
        //演员对CustomFragment UMA VEZ阙devemos recupera-LO科莫序列化,poderia之三墙裙投direto,MAS decidi manter assim对MELHOR entendimento做código
        CustomFragment片段=(CustomFragment)serializableExtra;
        FragmentTransaction英尺= slideMenuActivity.getSupportFragmentManager()调用BeginTransaction(); // Informa的阙estamos inicando UMAtransação新星
        ft.replace(R.id.dashboard,片段);
        ft.addToBackStack(NULL);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.commit();
        // Aplicamos作为alteraçõespermitindo阙Ø埃斯塔做片段seja尔迪posteriormente,utilizamosØStateLoss对otimizar
        //Øgerenciamento德MEMORIAËgarantir阙nenhum片段fique对traz porem SEM possibilitado德辑revisitado
        slideMenuActivity.toggle(); //褐simplesmente estamos escondendo O菜单
    }

我也试图与该code和没有区别:

 公共无效的onReceive(上下文的背景下,意图意图){
        序列化serializableExtra = intent.getSerializableExtra(CustomFragment.CUSTOM_FRAGMENT);
        CustomFragment片段=(CustomFragment)serializableExtra;
        片段oldFragment = slideMenuActivity.getSupportFragmentManager()findFragmentById(R.id.dashboard)。
        FragmentTransaction英尺;
        如果(oldFragment!= NULL){
            FT = slideMenuActivity.getSupportFragmentManager()调用BeginTransaction()。
            ft.remove(oldFragment);
            ft.commit();
        }
        。FT = slideMenuActivity.getSupportFragmentManager()调用BeginTransaction(); ft.add(R.id.dashboard,片段);
        ft.addToBackStack(NULL);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.commit();
        slideMenuActivity.toggle();
    }

在这里,你可以看到我的仪表盘布局:

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID /仪表板
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:背景=@色/白&GT;&LT; /&的FrameLayout GT;


解决方案

请考虑到 fragmentTransaction.replace 是(在写这篇的时间)越野车 - 见的https://$c$c.google.com/p /安卓/问题/细节?ID = 24674

你现在可以做的是


  • 要出演这个问题(在所提供的链接),

  • 想出了一个解决办法,直到谷歌已经固定。

我的解决方法是手动删除所有的相关片段(以 fragmentManager.getFragments的帮助())和正常添加新片段与 fragmentTransaction.add

I already searched for this issue on stackoverflow and found this two solutions:

Replacing a fragment with another fragment inside activity group
Fragment add or replace not working

But my code seems to be correct as you can check below, the comments on my code are in Portugues (Brazil):

This is my class MenuItemReceiver (Yes, I use a BroadcastReceiver to show my fragments), this already work to show my fragment content but when I try to replace it, the old fragment stays and the new one just appear above the old one.

    public void onReceive(Context context, Intent intent) {
        //Fragment que será carregado, este fragment foi repassado pelo SlideMenuAdapter ao ter um item clicado
        Serializable serializableExtra = intent.getSerializableExtra(CustomFragment.CUSTOM_FRAGMENT);
        //Cast para CustomFragment uma vez que devemos recupera-lo como Serializable, poderia ter dado cast direto, mas decidi manter assim para melhor entendimento do código
        CustomFragment fragment =  (CustomFragment) serializableExtra;
        FragmentTransaction ft = slideMenuActivity.getSupportFragmentManager().beginTransaction();//Informa que estamos inicando uma transação nova
        ft.replace(R.id.dashboard,fragment);
        ft.addToBackStack(null);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.commit();
        //Aplicamos as alterações permitindo que o estado do fragment seja perdido posteriormente, utilizamos o StateLoss para otimizar 
        //o gerenciamento de memória e garantir que nenhum fragment fique para traz porem sem possibilitado de ser "revisitado"
        slideMenuActivity.toggle();//Aqui simplesmente estamos escondendo o menu
    }

I have tried also with this code and have no difference:

    public void onReceive(Context context, Intent intent) {
        Serializable serializableExtra = intent.getSerializableExtra(CustomFragment.CUSTOM_FRAGMENT);
        CustomFragment fragment =  (CustomFragment) serializableExtra;
        Fragment oldFragment = slideMenuActivity.getSupportFragmentManager().findFragmentById(R.id.dashboard);
        FragmentTransaction ft;
        if(oldFragment!=null){
            ft = slideMenuActivity.getSupportFragmentManager().beginTransaction();
            ft.remove(oldFragment);
            ft.commit();
        }
        ft = slideMenuActivity.getSupportFragmentManager().beginTransaction();ft.add(R.id.dashboard,fragment);
        ft.addToBackStack(null);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.commit();
        slideMenuActivity.toggle();
    }

And here you can see my dashboard layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dashboard"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white" >

</FrameLayout>

解决方案

Please take into account that fragmentTransaction.replace is (at the time of writing this) buggy - see https://code.google.com/p/android/issues/detail?id=24674.

What you could do now is

  • to star the issue (at the link provided) and
  • come up with a workaround until Google has it fixed.

My workaround is to manually remove all relevant fragments (with the help of fragmentManager.getFragments()) and to add the new fragment normally with fragmentTransaction.add.

这篇关于FragmentTransaction更换不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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