【问题标题】:How to prevent opening a fragment from navigation drawer if that fragment is already opened如果该片段已打开,如何防止从导航抽屉打开该片段
【发布时间】:2016-10-08 13:57:21
【问题描述】:

如果片段已经打开,如何防止从导航抽屉打开片段,当片段打开并单击NavigationDrawer项时片段再次重新创建,那么如何检查片段是否已经打开?

 public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    if (id == R.id.fragmentA) {
         {   FragmentA fragment = new FragmentA();
             transaction.add(R.id.main_screen, fragment1, "MainFrag").addToBackStack(null);
         }

    } else if (id == R.id.fragmentB) {
        FragmentB fragment = new FragmentB();
        transaction.add(R.id.main_screen, fragment).addToBackStack(null);

    } else if (id == R.id.fragmentC) {
        FragmentC fragment = new FragmentC();
        transaction.replace(R.id.main_screen, fragment).addToBackStack(null);

    }

【问题讨论】:

    标签: android android-studio android-fragments navigation-drawer


    【解决方案1】:

    我是这样解决的:

    public boolean onNavigationItemSelected(MenuItem item) {
    
        int id = item.getItemId();
    
        Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
        if (id == R.id.n_1 && !(f instanceof MainFragment)) {
            MainFragment fragment = new MainFragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        } }
    

    【讨论】:

      【解决方案2】:

      例如我在片段B中,当我点击片段B时 再次从它重新创建的 NavigationDrawer,那么如何检查 片段是否已经显示在屏幕上?

      如果您有来自NavigationDrawer 的片段B 的引用,您可以致电mFragmentB.isAdded() 了解它是否已经显示。

      【讨论】:

      • 从 NavigationDarwer 中引用是什么意思?
      • 拥有引用意味着你可以通过你的方法onNavigationItemSelected(MenuItem item)访问对象fragmentB
      【解决方案3】:

      在添加片段时使用标签来替换它。

          transaction.replace(R.id.main_screen, fragment, tag);
      

      现在,当单击菜单项时,使用片段管理器使用标签获取可见片段:

          public boolean isFragmentCurrentlyVisible(String fragmentTag){
          Fragment fragmentToTest;
          fragmentToTest = fragmentManager.findFragmentByTag(fragmentTag);
          if(fragmentToTest!=null && fragmentToTest.isVisible())
              return true;
          return false;
      
      }
      

      根据上述方法返回的值,如果尚未添加,您可以添加片段。

      【讨论】:

        【解决方案4】:

        我也为此苦苦挣扎了一段时间,我正在使用导航组件和 Kotlin,所以最后我把它整理好了。这可能不是最好的解决方案,但它简单且有效,所以我想与您分享:

                    when (menuItem.itemId) {
        
                    R.id.nav_board -> {
                        if(findNavController(R.id.nav_host_fragment).currentDestination?.label != "listFragment")
                            findNavController(R.id.nav_host_fragment)
                                .navigate(R.id.action_global_listFragment)
                    }
        

        标签是navigation.xml中给出的目的地标签

        <navigation 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:id="@+id/navigation"
            app:startDestination="@id/listFragment">
        
            <fragment
                android:id="@+id/listFragment"
                android:name="at.example.app.ui.ListFragment"
                android:label="listFragment"
                tools:layout="@layout/fragment_list">
        ...
        

        所以这基本上只是检查当前目的地(我们所在的位置)是否是不应重新打开的目的地,并且仅在当前目的地是不同的目的地时导航。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多