切换 Fragment 后 BottomNavigation 不起作用

发布于 2025-01-09 03:15:19 字数 3952 浏览 0 评论 0原文

我是 Android Studio 新手,目前正在开发我的第一个应用程序。 我首先添加 Android Studio 提供的 BottomNavigationActivity。 片段应该显示一个项目列表,可以通过按钮添加该列表(这将打开一个新片段,可以在其中创建新项目并将其添加到列表中,通过单击按钮 -> 这会将用户带回列表) 我的问题是,在单击按钮添加项目后,我的导航栏停止工作。 这是我收到的消息: I/FragmentNavigator:忽略导航()调用:FragmentManager 已保存其状态

我有 2 个导航文件:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mobile_navigation"
    app:startDestination="@+id/navigation_kalender">

    <fragment
        android:id="@+id/navigation_habits"
        android:name="com.example.bottomtest.ui.habits.HabitsFragment"
        android:label="@string/title_habits"
        tools:layout="@layout/fragment_habits" />

    <fragment
        android:id="@+id/navigation_kalender"
        android:name="com.example.bottomtest.ui.kalender.KalenderFragment"
        android:label="@string/title_kalender"
        tools:layout="@layout/fragment_kalender" />

    <fragment
        android:id="@+id/navigation_notizen"
        android:name="com.example.bottomtest.ui.notizen.NotizenFragment"
        android:label="@string/title_notizen"
        tools:layout="@layout/fragment_notizen" />
</navigation>
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/notizen_navigation"
    app:startDestination="@id/notizenFragment">

    <fragment
        android:id="@+id/notizenBearbeitenFragment"
        android:name="com.example.bottomtest.ui.notizen.NotizenBearbeitenFragment"
        android:label="fragment_notizen_bearbeiten"
        tools:layout="@layout/fragment_notizen_bearbeiten" >
        <action
            android:id="@+id/action_notizenBearbeitenFragment_to_notizenFragment"
            app:destination="@id/notizenFragment" />
    </fragment>
    <fragment
        android:id="@+id/notizenFragment"
        android:name="com.example.bottomtest.ui.notizen.NotizenFragment"
        android:label="fragment_notizen"
        tools:layout="@layout/fragment_notizen" >
        <action
            android:id="@+id/action_notizenFragment_to_notizenBearbeitenFragment"
            app:destination="@id/notizenBearbeitenFragment" />
    </fragment>
</navigation>

这是我的 MainActivity OnCreate:

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        BottomNavigationView navView = findViewById(R.id.nav_view);

        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_habits, R.id.navigation_kalender, R.id.navigation_notizen)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);
    }

这是我在 Fragments 中的 OnViewCreated:

        binding.newNoteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NotizenBearbeitenFragment notizenBearbeitenFragment = new NotizenBearbeitenFragment();
                FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.container, notizenBearbeitenFragment);
                ft.addToBackStack(null);
                ft.commit();
            }
        });
    }

如果这是一个愚蠢的问题,我很抱歉,但我不能自己找到解决方案,非常感谢任何帮助!

Im new to Android Studio and currently working on my first App.
I have started by adding the BottomNavigationActivity provided by Android Studio.
The Fragments should display a List of items, which can be added via a Button (this opens a new Fragment where new Items can be created and added to the list by clicking a Button -> this brings the User back to the List)
My Problem is, that after I clicked the Button to add an Item, my Navbar stops working.
This is the Message i am getting:
I/FragmentNavigator: Ignoring navigate() call: FragmentManager has already saved its state

I have 2 Navigation files:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mobile_navigation"
    app:startDestination="@+id/navigation_kalender">

    <fragment
        android:id="@+id/navigation_habits"
        android:name="com.example.bottomtest.ui.habits.HabitsFragment"
        android:label="@string/title_habits"
        tools:layout="@layout/fragment_habits" />

    <fragment
        android:id="@+id/navigation_kalender"
        android:name="com.example.bottomtest.ui.kalender.KalenderFragment"
        android:label="@string/title_kalender"
        tools:layout="@layout/fragment_kalender" />

    <fragment
        android:id="@+id/navigation_notizen"
        android:name="com.example.bottomtest.ui.notizen.NotizenFragment"
        android:label="@string/title_notizen"
        tools:layout="@layout/fragment_notizen" />
</navigation>
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/notizen_navigation"
    app:startDestination="@id/notizenFragment">

    <fragment
        android:id="@+id/notizenBearbeitenFragment"
        android:name="com.example.bottomtest.ui.notizen.NotizenBearbeitenFragment"
        android:label="fragment_notizen_bearbeiten"
        tools:layout="@layout/fragment_notizen_bearbeiten" >
        <action
            android:id="@+id/action_notizenBearbeitenFragment_to_notizenFragment"
            app:destination="@id/notizenFragment" />
    </fragment>
    <fragment
        android:id="@+id/notizenFragment"
        android:name="com.example.bottomtest.ui.notizen.NotizenFragment"
        android:label="fragment_notizen"
        tools:layout="@layout/fragment_notizen" >
        <action
            android:id="@+id/action_notizenFragment_to_notizenBearbeitenFragment"
            app:destination="@id/notizenBearbeitenFragment" />
    </fragment>
</navigation>

This is my MainActivity OnCreate:

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        BottomNavigationView navView = findViewById(R.id.nav_view);

        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_habits, R.id.navigation_kalender, R.id.navigation_notizen)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);
    }

And this is my OnViewCreated in the Fragments:

        binding.newNoteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NotizenBearbeitenFragment notizenBearbeitenFragment = new NotizenBearbeitenFragment();
                FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.container, notizenBearbeitenFragment);
                ft.addToBackStack(null);
                ft.commit();
            }
        });
    }

Im sorry if this is a dumb question but i can't find a solution by myself, any help is greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨洒年华 2025-01-16 03:15:19

使用Navigation库时不要自己碰FragmentManager。

Navigation管理fragment之间的Navigation,这就是它的用途为了。

消息:

I/FragmentNavigator:忽略导航()调用:FragmentManager 有
已经保存了它的状态

androidx.navigation.fragment.FragmentNavigator#navigate 中定义
Fragment的状态是由Activity保存的。它通常在 Activity 进入 onStop 状态时出现。意味着离开当前Activity后不应该操作fragmentManger。 (但仍然可以使用 androidx.fragment.app.FragmentTransaction#commitAllowingStateLoss 来忽略警告,但重新创建 Activity 时不会恢复此状态。)

在您的场景中,我建议跳到下一个编辑、返回和刷新列表的活动

Do not touch the FragmentManager yourself when using the Navigation library.

Let Navigation manage Navigation between fragments, that's what it's used for.

The message:

I/FragmentNavigator: Ignoring navigate() call: FragmentManager has
already saved its state

is defined in the androidx.navigation.fragment.FragmentNavigator#navigate
The Fragment state is saved by the Activity. It usually appears when the Activity goes into the onStop state. Means should not be operated the fragmentManger after leaving the current Activity. (but still can use androidx.fragment.app.FragmentTransaction#commitAllowingStateLoss to ignore the warning , but this state will not be restored when the Activity is recreated.)

In your scenes, I recommend jumping to the next activity for editing, returning and refreshing the list

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文