【发布时间】:2021-11-01 18:32:57
【问题描述】:
我试图找出一个简单的解决方案,而不会使应用程序中的某些屏幕在使用 android 设备的后退按钮时无法返回,但我不确定哪种方法最适合我的情况。
应用程序没有使用导航组件,并且它的一个相当大的项目已经实现了,如果你按下后退按钮,就会出现登录等屏幕登录,它会带你回到登录。幸运的是,登录屏幕是一个活动,所以我可以在 startActivity(i); 之后执行 finish(); 并对其进行排序但是,登录后有多个片段会导致重复和重新输入值等问题只需按后退按钮。
我目前正在做的是 fragmentTransaction.addToBackStack(null); 在我做 commit(); 之前我尝试删除我不想要的 fragmentTransaction.addToBackStack(null); 并且它有点工作但它不会带你回到特定屏幕使用后退按钮,它会将您带到最后使用的fragmentTransaction.addToBackStack(null);,这在某些情况下会造成混乱。
我通常在做什么的例子:
Fragment enqFragment = new EnqFragment();
//Fragment findCustomerFragment = new AppInformationFragment();
FragmentTransaction transaction = requireActivity().getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, enquiryFragment);
transaction.addToBackStack(null); // I would comment this out if I did not want to go back here
transaction.commit();
我想要做的是从 A 到 B 再到 C,但是当我按下返回时,我想返回到 A,在某些情况下,当 C 转到 D 并且按下返回时,我想再次转到 A 或 B取决于场合。导航组件是最理想的方式,还是有更简单的解决方案来实现这一点,优缺点是什么?
【问题讨论】:
标签: java android navigation back-stack fragment-backstack