简体   繁体   English

片段之间的Android导航

[英]Android Navigation between fragments

I have a form divided in few fragments. 我的表格分为几个片段。 I call every fragment with: 我称每个片段为:

final FragmentManager fm = getSupportFragmentManager();
final FragmentTransaction ft = fm.beginTransaction();
if(fragment.equals(this.formOne) ||   fragment.equals(this.formTwo)) {
        ft.setCustomAnimations(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
    }

ft.replace(R.id.fragForm, fragment);
ft.addToBackStack(null);
ft.commit();

When I click on the back button it skips the previous Fragment and goes back to the Activity but with a blank screen. 当我单击“后退”按钮时,它会跳过前一个片段并返回到“活动”,但屏幕空白。

For example, I have 3 fragments : A - B - C 例如,我有3个片段:A-B-C

If I go to C and want to go back to the previous Activity, I click on the back button so I'm on B, I click again and I'm on A, and when I click again, I have a blank screen, I need to click another time to come back to the previous activity. 如果我转到C并想返回上一个活动,请单击“后退”按钮,以便在B上,再单击一次,然后在A上,再次单击时,我会出现空白屏幕,我需要再单击一次以返回上一个活动。

I don't understand why have I this blank screen on my Activity. 我不明白为什么我的活动上有这个空白屏幕。

I don't understand this in the developer documentation: 我在开发人员文档中不明白这一点:

Note: You should not add transactions to the back stack when the transaction is for horizontal navigation (such as when switching tabs) or when modifying the content appearance (such as when adjusting filters). 注意:当事务用于水平导航(例如,切换选项卡时)或修改内容外观(例如,调整过滤器时)时,请勿将事务添加到后堆栈中。 For more information, about when Back navigation is appropriate, see the Navigation design guide. 有关何时适合使用“后向导航”的更多信息,请参见“导航设计指南”。

If we can't use that, what is the solution? 如果我们不能使用它,解决方案是什么?

Don't add it to the backstack when you add fragment A. 添加片段A时,请勿将其添加到堆栈中。

ft.add(R.id.fragForm, fragmentA);
ft.commit();


ft.replace(R.id.fragForm, fragmentB);
ft.addToBackStack(null);
ft.commit();

ft.replace(R.id.fragForm, fragmentC);
ft.addToBackStack(null);
ft.commit();

If you don't have to Navigate to previous fragment don't add it to the backstack 如果您不必导航至上一个片段,则无需将其添加到后堆栈

Remove this 删除这个

 ft.addToBackStack(null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM