调用 .remove(fragment) 后片段未被删除
Fragment not being deleted after calling .remove(fragment)
我正在覆盖 onBackPressed
函数来更改我正在使用的应用程序的后退键行为。
如果 FragmentTransaction 包含带有特定标记的片段 qFrag
,那么我希望它为视图中的片段设置动画。
然而,在我删除片段后,它似乎仍然存在。因此,它永远不会达到我的 if 语句的 else
条件。
@Override
public void onBackPressed() {
// After pressing the back key a second time, questionFrag still has a value.
Fragment questionFrag = getSupportFragmentManager().findFragmentByTag("qFrag");
// If question fragment in the fragment manager
if (questionFrag != null) {
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.anim.slide_up, R.anim.slide_down)
.replace(R.id.main_menu_content_frame_layout, new Fragment(), "temp")
.remove(questionFrag)
.commit();
} else {
finish();
}
}
有人可以建议我如何从事务管理器中适当地删除片段吗?
试试这个,先调用 .remove,然后添加新片段(不是替换,因为 .remove 会清空容器),然后设置自定义动画:
if (questionFrag != null) {
getSupportFragmentManager()
.beginTransaction()
.remove(questionFrag)
.add(R.id.main_menu_content_frame_layout, new Fragment(), "temp")
.setCustomAnimations(R.anim.slide_up, R.anim.slide_down)
.commit();
System.out.println("questionFrag removed successfully");
finish()
} else {
finish();
}
您可以添加system.out来检查代码到达了哪一点,这是发现错误的好方法。如果您没有在日志中看到打印输出,您就会知道 FragmentManager 代码没有正确执行。
关于本主题中的片段,您需要了解两件事:
1 - 当你给一个片段一个标签并将它添加到你最肯定的返回堆栈时,这个:
Fragment questionFrag = getSupportFragmentManager().findFragmentByTag("qFrag");
只有 returns 如果您调用 popBackStack()
为 null。由于您没有调用它,即使您对其调用 remove,该片段仍存储在后台堆栈中。
2-这段代码
.replace(R.id.main_menu_content_frame_layout, new Fragment(), "temp")
.remove(questionFrag)
是冗余的,因为当您调用 replace
时,您是在给定容器中的每个片段上调用 remove,然后添加新片段,这样
.replace(R.id.main_menu_content_frame_layout, new Fragment(), "temp")
够了。
现在,有两种方法可以解决您的问题,它们是:
替换后弹出返回堆栈:
if (questionFrag != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_menu_content_frame_layout, new Fragment(), "temp")
.setCustomAnimations(R.anim.slide_up, R.anim.slide_down)
.commit();
getSupportFragmentManager().popBackStack();
System.out.println("questionFrag removed successfully");
finish()
} else {
finish();
}
或者,不是弹出后台堆栈,而是验证片段是否在容器中
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.main_menu_content_frame_layout);
if(fragment instanceOf QuestionFragment){
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_menu_content_frame_layout, new Fragment(), "temp")
.setCustomAnimations(R.anim.slide_up, R.anim.slide_down)
.commit();
getSupportFragmentManager().popBackStack();
System.out.println("questionFrag removed successfully");
finish()
} else {
finish();
}
我正在覆盖 onBackPressed
函数来更改我正在使用的应用程序的后退键行为。
如果 FragmentTransaction 包含带有特定标记的片段 qFrag
,那么我希望它为视图中的片段设置动画。
然而,在我删除片段后,它似乎仍然存在。因此,它永远不会达到我的 if 语句的 else
条件。
@Override
public void onBackPressed() {
// After pressing the back key a second time, questionFrag still has a value.
Fragment questionFrag = getSupportFragmentManager().findFragmentByTag("qFrag");
// If question fragment in the fragment manager
if (questionFrag != null) {
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.anim.slide_up, R.anim.slide_down)
.replace(R.id.main_menu_content_frame_layout, new Fragment(), "temp")
.remove(questionFrag)
.commit();
} else {
finish();
}
}
有人可以建议我如何从事务管理器中适当地删除片段吗?
试试这个,先调用 .remove,然后添加新片段(不是替换,因为 .remove 会清空容器),然后设置自定义动画:
if (questionFrag != null) {
getSupportFragmentManager()
.beginTransaction()
.remove(questionFrag)
.add(R.id.main_menu_content_frame_layout, new Fragment(), "temp")
.setCustomAnimations(R.anim.slide_up, R.anim.slide_down)
.commit();
System.out.println("questionFrag removed successfully");
finish()
} else {
finish();
}
您可以添加system.out来检查代码到达了哪一点,这是发现错误的好方法。如果您没有在日志中看到打印输出,您就会知道 FragmentManager 代码没有正确执行。
关于本主题中的片段,您需要了解两件事:
1 - 当你给一个片段一个标签并将它添加到你最肯定的返回堆栈时,这个:
Fragment questionFrag = getSupportFragmentManager().findFragmentByTag("qFrag");
只有 returns 如果您调用 popBackStack()
为 null。由于您没有调用它,即使您对其调用 remove,该片段仍存储在后台堆栈中。
2-这段代码
.replace(R.id.main_menu_content_frame_layout, new Fragment(), "temp")
.remove(questionFrag)
是冗余的,因为当您调用 replace
时,您是在给定容器中的每个片段上调用 remove,然后添加新片段,这样
.replace(R.id.main_menu_content_frame_layout, new Fragment(), "temp")
够了。
现在,有两种方法可以解决您的问题,它们是:
替换后弹出返回堆栈:
if (questionFrag != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_menu_content_frame_layout, new Fragment(), "temp")
.setCustomAnimations(R.anim.slide_up, R.anim.slide_down)
.commit();
getSupportFragmentManager().popBackStack();
System.out.println("questionFrag removed successfully");
finish()
} else {
finish();
}
或者,不是弹出后台堆栈,而是验证片段是否在容器中
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.main_menu_content_frame_layout);
if(fragment instanceOf QuestionFragment){
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_menu_content_frame_layout, new Fragment(), "temp")
.setCustomAnimations(R.anim.slide_up, R.anim.slide_down)
.commit();
getSupportFragmentManager().popBackStack();
System.out.println("questionFrag removed successfully");
finish()
} else {
finish();
}