简体   繁体   English

Android导航组件如何保存BottomNavigationBar片段state

[英]Android navigation component how save BottomNavigationBar fragments state

I'm used the NavigationExtensions.kt class from the NavigationAdvancedSample to implement the BottomNavigationBar navigation.我使用 NavigationAdvancedSample 中的 NavigationExtensions.kt class 来实现 BottomNavigationBar 导航。 as the following:如下:

 bottomNavigationView = findViewById(R.id.bottom_nav)
        val navGraphIds = listOf(R.navigation.home, R.navigation.category, R.navigation.cart, R.navigation.account, R.navigation.jak)
        // Setup the bottom navigation view with a list of navigation graphs
        val controller = bottomNavigationView?.setupWithNavController(
                navGraphIds = navGraphIds,
                fragmentManager = supportFragmentManager,
                containerId = R.id.nav_host_container,
                intent = intent
        )

        // Whenever the selected controller changes, setup the action bar.
        controller?.observe(this, Observer { navController ->
            setupActionBarWithNavController(navController)
        })
        currentNavController = controlle

I'm trying to save the fragment states for every tap, but I don't know how to do it.我正在尝试为每次点击保存片段状态,但我不知道该怎么做。 All classes and fragments are written in java, only the main activity and NavigationExtensions in Kotlin.所有类和片段都写在 java 中,只有 Kotlin 中的主要活动和 NavigationExtensions。 Navigation implementations:导航实现:

implementation "androidx.navigation:navigation-fragment:2.2.0-alpha03"
implementation "androidx.navigation:navigation-ui:2.2.0-alpha03"
implementation "androidx.navigation:navigation-fragment-ktx:2.2.0-alpha03"
implementation "androidx.navigation:navigation-ui-ktx:2.2.0-alpha03"

and this is one of my Fragment onCreateView method:这是我的片段 onCreateView 方法之一:

@Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mViewModel = ViewModelProviders.of(this).get(HomePageViewModel.class);
        binding = DataBindingUtil.inflate(inflater, R.layout.home_page_fragment, container, false);
        binding.setLifecycleOwner(this);
        mViewModel.init();
        binding.setViewModel(mViewModel);
        setHasOptionsMenu(true);
        ((MainActivity) getActivity()).homeToolbarWithImage();
        ShowMainList();
        binding.swiperefresh.setOnRefreshListener(this);
        return binding.getRoot();
    }

Can anyone help me to slove the problem?谁能帮我解决这个问题?

You can use "FragmentPagerAdapter" for saving the state of diff fragments.您可以使用“FragmentPagerAdapter”来保存差异片段的 state。 Add your fragments using the "FragmentPagerAdapter".使用“FragmentPagerAdapter”添加您的片段。 Also don't worry, if you are unable to understand the java classes.也不要担心,如果您无法理解 java 类。 You can still you these directly within your kotlin classes.您仍然可以直接在 kotlin 类中使用这些。 Kotline provides interoperability because of which you can use java and kotiln classes together in same project. Kotline 提供了互操作性,因此您可以在同一个项目中同时使用 java 和 kotiln 类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM