如果不可见,如何关闭片段

我是一名新的应用程序开发人员,我在主要活动中将其添加到我的应用程序底部导航栏中,它工作正常,但是当我尝试从底部导航栏移至TabLayout,ViewPager的片段时,我的应用程序变得固定了,我明白为什么发生这种情况是因为每次第一次单击botton来查看它时,我都会再次创建片段。

但是我不知道如何解决这个问题。但是我认为如果每次用户单击另一个按钮时都将其关闭将解决此问题。

如果有人知道解决此问题,请帮助我。

我从这项活动

public class NafMain extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_naf_main);

            BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
            BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
            bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                    Fragment selectedFragment = null;

                    switch (menuItem.getItemId()) {
                        case R.id.nav_home:
                            selectedFragment = new MainActivityOderLIstFargmant();//here problem

                            break;
                        case R.id.nav_favorites:
                            selectedFragment = new FragmentHome();
                            break;
                        case R.id.nav_search:
                            selectedFragment = new FragmentProfile();
                            break;
                        case R.id.nav_s:
                            selectedFragment = new MainActivity();
                            break;
                    }
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                            selectedFragment).commit();
                    return true;

                }
            });


        }
    }


要这个片段..

public class MainActivityOderLIstFargmant extends Fragment {
   TabLayout tabLayout;
   ViewPager viewPager;
   PageAdapterOrderList pageAdapterOrderList;
   TabItem tabChats;
   TabItem tabCalls;

   @Nullable
   @Override
   public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       // return inflater.inflate(R.layout.fragment_hoome, container, false);
       setHasOptionsMenu(true);
       View rootView = inflater.inflate(R.layout.activity_mani_order_list, container, false);

       tabLayout = rootView.findViewById(R.id.tablayout);
       tabChats = rootView.findViewById(R.id.tabChats);
       tabCalls = rootView.findViewById(R.id.tabCalls);
       viewPager = rootView.findViewById(R.id.viewPager);

       pageAdapterOrderList = new PageAdapterOrderList(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
       viewPager.setAdapter(pageAdapterOrderList);
       viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));


       return rootView;

   }

}


我从打开的MainActivityOderLIstFargmant查看此片段

public class Fragmentlisttwo extends Fragment {


        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

            setHasOptionsMenu(true);
            View rootView = inflater.inflate(R.layout.fragment_o_two, container, false);

            return rootView;

        }

我通过名称(搜索)在最后一个按钮中将此示例源代码添加到我的问题中

https://gofile.io/d/Or9fTf

评论
miste
miste

You should using getChildFragmentManager() instead of getActivity().getSupportFragmentManager()

pageAdapterOrderList = new PageAdapterOrderList(getChildFragmentManager(), tabLayout.getTabCount());
点赞
评论