Привет, я уже реализовал Jetpack Navigation + BottomNavigationView + фрагменты, но я не знаю, это лучший способ сделать это или нет?
Ресурс меню BottomNavigationView:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/fragmentFirst"
android:icon="@drawable/ic_navigation_first"
android:title="@string/navigation_first"/>
<item
android:id="@+id/fragmentSecond"
android:icon="@drawable/ic_navigation_first"
android:title="@string/navigation_first"/>
</menu>
Макет ActivityMain:
<androidx.coordinatorlayout.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?colorSurface">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<fragment
android:id="@+id/navigation_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:navGraph="@navigation/navigation"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:itemTextColor="@color/navigation_text"
app:menu="@menu/navigation"/>
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
MainActivity класс:
@BindView(R.id.navigation)
BottomNavigationView mNavigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
NavigationUI.setupWithNavController(mNavigation, Navigation.findNavController(this, R.id.navigation_host_fragment));
}
И работает нормально, за исключением того, что фрагменты всегда воссоздают. Какой лучший способ реализовать ту же логику с использованием инфраструктуры навигации, но чтобы фрагменты не были воссозданы?