我有一个主要活动,其中有一个recyclerview,我从中打开一个新片段,比如说片段1,现在在片段1中我有一个场景,我必须重新加载/替换相同的片段,这是此方法的问题
FragmentTransaction transaction = requireActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, new make_everything_ok_btn());
transaction.addToBackStack(null)
transaction.commit();
如果重新加载的场景出现多次,我确信会出现 100%,现在这是在后台添加同一片段的多个重复项
例如,如果我必须重新加载片段 3 次,我必须按返回 3 次才能导航回主活动屏幕
现在您可以建议的一个可能的解决方案是删除
transaction.addToBackStack(null);
这不会添加后退堆栈,现在在这种情况下,将不会导航到主活动,而是按 2 次后退后应用程序将关闭
所以为了解决这个问题,我有一个想法,如果不是静态地替换相同的片段,我只是在后台堆栈中获取最新添加的片段,例如它的fragment1/make_everything_ok_btn(),并用它自己替换后台堆栈的相同实例现在我不会添加 addToBackstack ,现在我从这个想法中了解到的是,这将用自身替换现有片段,而不向 backstacl 添加新的相同片段
我在这里尝试了一个代码
FragmentManager fragmentManager = requireActivity().getSupportFragmentManager();
int backStackEntryCount = fragmentManager.getBackStackEntryCount();
// Check if there are fragments in the back stack
if (backStackEntryCount > 0) {
// Get the name of the latest fragment in the back stack
String latestFragmentName = fragmentManager.getBackStackEntryAt(backStackEntryCount - 1).getName();
// Check if the current fragment is different from the latest fragment in the back stack
Fragment currentFragment = fragmentManager.findFragmentById(R.id.fragment_container);
if (currentFragment != null && !currentFragment.getClass().getSimpleName().equals(latestFragmentName)) {
// Get the latest fragment instance from the back stack
Fragment latestFragmentInstance = fragmentManager.findFragmentByTag(latestFragmentName);
// Replace the current fragment with a new instance of the latest fragment
if (latestFragmentInstance != null) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_container, latestFragmentInstance);
transaction.commit();
}else {
Log.d("backstack","latest fragment is null");
}
}else {
Log.d("backstack","Not same class");
}
}else {
Toast.makeText(getContext(), String.valueOf(backStackEntryCount),Toast.LENGTH_SHORT).show();
Log.d("backstack", String.valueOf(backStackEntryCount));
}
但是这段代码不起作用,它不重新加载最新的片段,而是打印此日志 Log.d("backstack","latestfragment is null");
这意味着可用的后台堆栈中没有可用的最新片段,这很奇怪,因为我已从 mainactivity 导航到fragment1 并且我当前看到了fragment1
请有人能帮我吗
请不要问为什么我要重新加载片段,这里解释起来很复杂
这也是从主活动中的 rcyclerview 打开片段的代码
private void openFragmentBasedOnPosition(int position) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment fragment = null;
String fragmentTag = null;
switch (position) {
case 0:
// Open Fragment associated with the first item
fragment = new make_everything_ok_btn();
fragmentTag = "make_everything_ok_btn";
break;
case 1:
// Open Fragment associated with the second item
fragment = new esc_ratrace_btn();
fragmentTag = "esc_ratrace_btn";
break;
case 2:
// Open Fragment associated with the second item
fragment = new delete_btn();
fragmentTag = "delete_btn";
break;
case 3:
// Open Fragment associated with the second item
fragment = new alttablife_btn();
fragmentTag = "alttablife_btn";
break;
case 4:
// Open Fragment associated with the second item
fragment = new givemespace_btn();
fragmentTag = "givemespace_btn";
break;
default:
break;
}
if (fragment != null) {
transaction.replace(R.id.fragment_container, fragment, fragmentTag);
transaction.addToBackStack(null);
transaction.commit();
}
}
编辑1:-我也尝试过这个解决方案
Fragment frg;
frg = requireActivity().getSupportFragmentManager().findFragmentByTag("make_everything_ok_btn");
final FragmentTransaction ft = requireActivity().getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
但它似乎不起作用,并且不重新加载片段,或者也许我可以说没有根据this
刷新使用
从后台堆栈中删除最新的片段requireActivity().getSupportFragmentManager().popBackStack();
然后使用原始代码
FragmentTransaction transaction = requireActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, new make_everything_ok_btn());
transaction.addToBackStack(null);
transaction.commit();
给了我想要的输出。
组合后的代码如下所示:
requireActivity().getSupportFragmentManager().popBackStack();
FragmentTransaction transaction = requireActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, new make_everything_ok_btn());
transaction.addToBackStack(null);
transaction.commit();