简体   繁体   English

应用程序(屏幕)旋转后的 Android BottomNavigation 片段显示/隐藏

[英]Android BottomNavigation fragment show/hide after App (screen) rotate

I am trying to simulate App (screen) rotate, but after the screen is rotated, the BottomNavigation fragments won't work anymore.我正在尝试模拟 App(屏幕)旋转,但在屏幕旋转后,BottomNavigation 片段将不再起作用。

I have the following Array / vars我有以下数组/变量

private Fragment[] fragments = new Fragment[]{new HomeFragment(), new MapFragment(), new SavedFragment(), new NotificationsFragment()};
private int selected = -1;

In onCreateView I am calling...onCreateView我打电话...

// open first Fragment when app starts
if (savedInstanceState == null) switchFragment(0, ShoutsHomeFragment.TAG); 
else selected = savedInstanceState.getInt(SELECTED_FRAGMENT);

switchFragment looks like switchFragment看起来像

private void switchFragment(int index, String tag) {
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    Fragment fragment = fragmentManager.findFragmentByTag(tag);
    // creating for the first time
    if (fragment == null) transaction.add(R.id.tab_fragment_container, fragments[index], tag);
    if (selected >= 0) transaction.hide(fragments[selected]);   // <--- don't work
    if (fragment != null) transaction.show(fragment);           // <--- don't work
    transaction.commit();
    selected = index;
}

So, transaction.hide and transaction.show is not working after rotating the screen, it stays on the same Fragment when I tap on other items on the BottomNavigation因此, transaction.hidetransaction.show在旋转屏幕后不起作用,当我点击 BottomNavigation 上的其他项目时,它保持在同一个 Fragment 上

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.navigation_shouts_home:
            switchFragment(0, HomeFragment.TAG);
            return true;
        case R.id.navigation_heat_map:
            switchFragment(1, MapFragment.TAG);
            return true;
        case R.id.navigation_loved:
            switchFragment(2, SavedFragment.TAG);
            return true;
        case R.id.navigation_notifications:
            switchFragment(3, NotificationsFragment.TAG);
            return true;
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
    outState.putInt(SELECTED_FRAGMENT, selected);
    super.onSaveInstanceState(outState);
}

I programmed it this way so that I can retain Fragments (scroll) position when switching between items on BottomNavigation.我以这种方式对其进行了编程,以便在 BottomNavigation 上的项目之间切换时可以保留 Fragments(滚动)位置。 So I want to show/hide instead of creating new Instance every time.所以我想show/hide而不是每次都创建新的实例。 Any help is appreciated.任何帮助表示赞赏。

When you rotate the screen, all of your activities and all of your non-retained fragments are put through the whole lifecycle.当您旋转屏幕时,您的所有活动和所有未保留的片段都将贯穿整个生命周期。 This means your private Fragment[] fragments... is also recreated.这意味着您的private Fragment[] fragments...也被重新创建。 Your Fragments in your list are no longer the fragments in the FragmentManager .您列表中的FragmentManager不再是FragmentManager中的FragmentManager This means hide is not going to work the way you have it being called and the top-most Fragment will always be the one that is visible.这意味着 hide 不会按照您调用它的方式工作,并且最顶层的 Fragment 将始终是可见的。 If you use the TAG method, it will probably work.如果您使用 TAG 方法,它可能会起作用。


String getFragmentTagByIndex(final int index) {
    switch (index) {
       // Make return tag.
    }
}

{
// inside switchFragment
    final Fragment visibleFragment = transaction.findFragmentByTag(getFragmentTagByIndex(selected));
    transaction.hide(visibleFragment);
}

You are essentially trying to implement a ViewPager with a FragmentPagerAdapter I would recommend you use the FragmentPagerAdapter您实际上是在尝试使用FragmentPagerAdapter实现ViewPager我建议您使用 FragmentPagerAdapter

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

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