gpt4 book ai didi

android - FragmentTransaction 替换不工作

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:04:41 25 4
gpt4 key购买 nike

我已经在 stackoverflow 上搜索过这个问题并找到了这两个解决方案:

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

但是我的代码似乎是正确的,您可以在下面查看,我的代码上的注释是葡萄牙语(巴西):

这是我的类 MenuItemReceiver(是的,我使用 BroadcastReceiver 来显示我的 fragment ),这已经可以显示我的 fragment 内容但是当我尝试替换它时,旧 fragment 保留下来,新 fragment 出现在旧 fragment 之上一个。

    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
}

我也试过这段代码,没有什么区别:

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

在这里您可以看到我的仪表板布局:

<?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>

最佳答案

请注意 fragmentTransaction.replace 是(在撰写本文时)错误 - 参见 https://code.google.com/p/android/issues/detail?id=24674 .

你现在可以做的是

  • 为问题加注星标(在提供的链接中)和
  • 在 Google 修复它之前想出一个解决方法。

我的解决方法是手动删除所有相关 fragment (在 fragmentManager.getFragments() 的帮助下)并使用 fragmentTransaction 通常添加新 fragment 。添加

关于android - FragmentTransaction 替换不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20974173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com