[英]Bottom navigation bar stop a fragment running when another one is opened
So I have no idea how to do this and even where to start, so if someone would be patient enough to explain how to do this I would be appreciated.所以我不知道如何做到这一点,甚至不知道从哪里开始,所以如果有人有足够的耐心解释如何做到这一点,我将不胜感激。 I already looked up on google but what I found was too difficult to understand ( and I am pretty sure that it can be done way more easily.
我已经在谷歌上查过了,但我发现很难理解(而且我很确定它可以更容易地完成。
I have 4 fragments (4 tabs on a bottom navigation bar) and I want to have only 1 running at the time.我有 4 个片段(底部导航栏上有 4 个选项卡),我希望当时只运行 1 个。 Now, I am pretty sure that I should use FragmentManager but I don't really know how to start to implement in my program.
现在,我很确定我应该使用 FragmentManager 但我真的不知道如何开始在我的程序中实现。
Do I have to write all the FragmentTransaction code in the mainActivity?我必须在 mainActivity 中编写所有 FragmentTransaction 代码吗? and after that how to keep goin?
之后如何继续? Is there a example code by any chance?
是否有示例代码?
Disclaimer: I am a total noob in the android studio environment and I am also a student and I almost do everything as a school project.免责声明:我在 android 工作室环境中是一个完全的菜鸟,我也是一名学生,我几乎把所有事情都作为一个学校项目。
You Can try this in your code.你可以在你的代码中试试这个。 On the onNavigationItemSelected
在 onNavigationItemSelected
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = new HomeFragment();
break;
case R.id.navigation_dashboard:
fragment = new DashboardFragment();
break;
case R.id.navigation_notifications:
fragment = new NotificationsFragment();
break;
case R.id.navigation_profile:
fragment = new ProfileFragment();
break;
}
return loadFragment(fragment);
}
private boolean loadFragment(Fragment fragment) {
//switching fragment
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
return true;
}
return false;
}
I hope this can help You!我希望这可以帮助你!
Thank You.谢谢你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.