[英]How does 'default' bottom navigation view changes fragments?
I've seen many articles how to implement bottom navigation view, but none of those solutions looked like one from new project created with Bottom Navigation Activity: 我看过很多文章如何实现底部导航视图,但是这些解决方案都不像使用底部导航活动创建的新项目中的解决方案:
BottomNavigationView navView = findViewById(R.id.nav_view);
AppBarConfiguration.Builder(
R.id.navigation_timer, R.id.navigation_presets)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
There are no transactions, no FragmentManager
etc. 没有交易,没有FragmentManager
等。
Reason I'm asking is because I'm trying to implement button on Fragment2 (PresetsFragment), which after click sets values and switches view to Fragment1 (TimerFragment). 我问的原因是因为我正在尝试在Fragment2(PresetsFragment)上实现按钮,单击后会设置值并将视图切换到Fragment1(TimerFragment)。 This is implementation of interface in MainActivity: 这是MainActivity中接口的实现:
@Override
public void updateCountdowns(final int round_time, final int break_time)
{
TimerFragment timer = (TimerFragment)getSupportFragmentManager().findFragmentById(R.id.navigation_timer);
if (timer != null) {
timer.updateCountdowns(round_time, break_time);
} else
{
TimerFragment new_timer = new TimerFragment();
Bundle args = new Bundle();
args.putInt("round_time_bundle", round_time);
args.putInt("break_time_bundle", break_time);
new_timer.setArguments(args);
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.replace(R.id.navigation_presets, new_timer, "new_timer")
.addToBackStack(null)
.commit();
}
}
After adding logs I can see that interface works and new `TimerFragment' is created - but fragment itself doesn't change. 添加日志后,我可以看到该接口可以正常工作,并且创建了新的“ TimerFragment”-但片段本身没有改变。
Actually comment of John Joe helped me a lot. 实际上,对约翰·乔的评论对我有很大帮助。 There is no reason to use interface between fragments if I use Navigation
. 如果使用Navigation
则没有理由在片段之间使用接口。
Bundle args = new Bundle();
args.putInt("round_time_bundle", round_time);
args.putInt("break_time_bundle", break_time);
Navigation.findNavController(view).navigate(R.id.navigation_timer, args);
In my example this is all that is needed. 在我的示例中,这就是所需要的。 Thank you. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.