1新建类 MyKeepStateNavigator
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavDestination;
import androidx.navigation.NavOptions;
import androidx.navigation.Navigator;
import androidx.navigation.fragment.FragmentNavigator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import kotlin.jvm.internal.Intrinsics;
@Navigator.Name("keep_state_fragment")
public class MyKeepStateNavigator extends FragmentNavigator {
private Context context;
private FragmentManager manager;
private int containerId;
public MyKeepStateNavigator(@NonNull Context context, @NonNull FragmentManager manager, int containerId) {
super(context, manager, containerId);
this.context = context;
this.manager = manager;
this.containerId = containerId;
}
@Nullable
@Override
public NavDestination navigate(@NonNull Destination destination, @Nullable Bundle args, @Nullable NavOptions navOptions, @Nullable Navigator.Extras navigatorExtras) {
String tag = String.valueOf(destination.getId());
FragmentTransaction transaction = manager.beginTransaction();
boolean initialNavigate = false;
Fragment currentFragment = manager.getPrimaryNavigationFragment();
if (currentFragment != null) {
// transaction.detach(currentFragment);
transaction.hide(currentFragment);
} else {
initialNavigate = true;
}
Fragment fragment = manager.findFragmentByTag(tag);
if (fragment == null) {
String className = destination.getClassName();
fragment = manager.getFragmentFactory().instantiate(context.getClassLoader(), className);
transaction.add(containerId, fragment, tag);
} else {
// transaction.attach(fragment);
transaction.show(fragment);
}
transaction.setPrimaryNavigationFragment(fragment);
transaction.setReorderingAllowed(true);
transaction.commitNow();
return initialNavigate destination : null;
}
}
2,去掉你的MainAcitity的布局文件中
注意,使用自定义 Navigator 的时候 navigation graph 需要把 fragment 节点名称改为 keep_state_fragment,并且在承载的 Activity 中进行设置并且还需要把 Activity 布局文件中 fragment 的 navGraph 属性移除。
3,在MainActivity中这么使用
BottomNavigationView navView = findViewById(R.id.nav_view);
initPermission();
// AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
// R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_gif, R.id.navigation_notifications)
// .build();
navController = Navigation.findNavController(this, R.id.nav_host_fragment);
// NavigationUI.setupWithNavController(navView, navController);
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
MyKeepStateNavigator navigator = new MyKeepStateNavigator(this, navHostFragment.getChildFragmentManager(), R.id.nav_host_fragment);
navController.getNavigatorProvider().addNavigator(navigator);
navController.setGraph(R.navigation.mobile_navigation);
NavigationUI.setupWithNavController(navView, navController);