Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 移除碎片并更换新的碎片_Android_Android Fragments - Fatal编程技术网

Android 移除碎片并更换新的碎片

Android 移除碎片并更换新的碎片,android,android-fragments,Android,Android Fragments,也许这个主题是重复的,而且有很多关于它的话题。但是我检查了所有的问题,没有得到答案 我有一个片段在FrameLayout上膨胀,当我点击特定的按钮时,我必须移除当前的一个并用另一个替换它。。但这个过程不起作用。首先,碎片也膨胀了,然后我点击按钮,碎片被移除。但另一个是创造出来的。它不会出现在屏幕上。尽管调用了onCreateView方法 这是我的密码: 在尝试替换之前,不要移除碎片 要替换的参数还有replace int containerViewId,Fragment Fragment。从第一

也许这个主题是重复的,而且有很多关于它的话题。但是我检查了所有的问题,没有得到答案

我有一个片段在FrameLayout上膨胀,当我点击特定的按钮时,我必须移除当前的一个并用另一个替换它。。但这个过程不起作用。首先,碎片也膨胀了,然后我点击按钮,碎片被移除。但另一个是创造出来的。它不会出现在屏幕上。尽管调用了onCreateView方法

这是我的密码:


在尝试替换之前,不要移除碎片


要替换的参数还有replace int containerViewId,Fragment Fragment。从第一行看,frameId似乎是要替换的片段的id。frameId应改为包含FrameLayout的id。

此处所做的是:

正在从容器中检索碎片A。 从容器中移除碎片A。然后是承诺。 再次用FragmentA替换空容器的内容物nothing。然后是承诺。 我在这里看到两个问题:

您正在删除该片段,并承诺因此该片段现在将通过top、ondestory等生命周期方法进行处理,然后您尝试在replace调用中再次添加它。您不应该添加已经删除的片段,因为它将被活动销毁。这可能解释了你在屏幕上看到的口吃;新的片段在销毁之前添加到容器中。 更换前无需拆卸。replace将删除容器中的所有片段,然后添加新片段。 说明新片段实例化和删除冗余的解决方案:

Fragment fragment1 = getSupportFragmentManager().findViewById(R.id.fragment_container);
if(fragment1 != null) {
    getSupportFragmentManager().remove(fragment1).commit(); //Unnecessary, as replace() will remove this anyway. Once we call this, we cannot use fragment1 again as its onDestroy() method is called. 
    Fragment fragment2 = new TestFragment();
    //If we use fragment2 here, the fragment is replaced on the screen. If I use fragment1, the fragment disappears (not replaced). 
    getSupportFragmentManager().replace(R.id.fragment_container, fragment2).commit();
}

为什么要移除碎片?只要替换它。我想刷新片段,我试图使用附加和分离,但我在我的应用程序上遇到了这个技术的问题。如果我直接使用replace,那么片段不会调用onCreateView方法。您在做什么,需要通过删除并重新添加片段来刷新它??在任何情况下,这可能会产生一个可见的闪烁。我的片段中有一个对象保存捆绑数据,我从另一个片段更新它,因此我希望在更新它时,片段调用onCreateView以用新的oneMohamed替换以前的状态,也许这篇文章的公认答案会有所帮助::frameId实际上似乎是正确的。findFragmentById方法接受一个整数资源,该资源标识我们要从中检索片段的容器。我之前已经替换了它,替换代码为else语句,但我认为我不需要提及它的代码,因为它对我的问题不重要。amm,我试图为一个片段创建一个新实例,但我得到了相同的结果,所以我认为问题与片段生命周期无关。另外,如果我直接使用replace方法,片段将保持原样,不会再次调用片段生命周期,因为我认为这是因为片段实例已经存在。您在哪里创建新实例?我尝试了你的代码,当片段被删除后,它就无法再次使用了。我在clickListener中创建了一个新实例,它可以正常工作;片段在屏幕上被替换:当我使用新实例并将特定对象传递给它并传递给替换函数时,该对象在片段内将为null!!我仍然不知道原因,你可以检查这个问题,你会明白我的意思
Fragment fragment1 = getSupportFragmentManager().findViewById(R.id.fragment_container);
if(fragment1 != null) {
    getSupportFragmentManager().remove(fragment1).commit(); //Unnecessary, as replace() will remove this anyway. Once we call this, we cannot use fragment1 again as its onDestroy() method is called. 
    Fragment fragment2 = new TestFragment();
    //If we use fragment2 here, the fragment is replaced on the screen. If I use fragment1, the fragment disappears (not replaced). 
    getSupportFragmentManager().replace(R.id.fragment_container, fragment2).commit();
}