切换 Fragment 后 BottomNavigation 不起作用
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用Navigation库时不要自己碰FragmentManager。
让Navigation管理fragment之间的Navigation,这就是它的用途为了。
消息:
在
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:
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