简体   繁体   English

底部导航栏在打开另一个片段时停止运行

[英]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.

相关问题 使用底部导航栏从一个片段移动到另一个片段时重置数据 - Data reset when moving from one to another fragment using Bottom Navigation Bar 尝试从底部导航栏更改片段时出错 - Error coming when trying to change fragment from bottom navigation bar 通过单击一个片段中的按钮并更改android studio中的底部导航图标,从一个片段重定向到另一个片段 - redirection from one fragment to another by clicking a button in one fragment together with changing the bottom navigation icon in android studio 在底部导航栏中保存片段 state - save fragment state in a bottom navigation bar Android Java 底部导航栏片段未显示 - Android Java Bottom Navigation Bar Fragment not showing 底部导航栏不在片段内工作 - Bottom Navigation Bar not working inside fragment 如何从底部导航栏打开本身在片段中的片段? - How to open a fragment from bottom navigation bar which is itself in a fragment? 通过底部导航栏将数据从活动传递到片段 - passing data from from an activiy to a fragment withn bottom navigation bar 底部导航栏和 NestedScroll View 隐藏了我的片段活动 - Bottom Navigation Bar and NestedScroll View hide my fragment activity 如何实现底部导航栏事务不是碎片,而是活动 class? - How to Implementing bottom navigation bar transaction not to fragment , however to Activity class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM