【问题标题】:Checking if a view exists before loading a fragment在加载片段之前检查视图是否存在
【发布时间】:2019-01-18 07:03:07
【问题描述】:

我想在加载片段之前查看视图是否存在。在我加载片段的方法中:

 public void loadFragment(Fragment fragment){
    FragmentManager fragmentManager = Objects.requireNonNull(getActivity()).getSupportFragmentManager();

    // Begin Fragment transaction.
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();// Replace the layout holder with the required Fragment object.


    fragmentTransaction.replace(R.id.outbound_dashboard_hourly_queued, fragment);
    // Commit the Fragment replace action.
    fragmentTransaction.commit();

}

我想要的是在加载之前检查R.id.outbound_dashboard_hourly_queued 是否存在

我该怎么做?

【问题讨论】:

  • 你的意思是如果相同的片段退出或任何其他片段?
  • if(findViewById(R.id.outbound_dashboard_hourly_queued)!=null) 然后做你的工作

标签: android android-fragments fragmentmanager


【解决方案1】:

只需执行findViewById(R.id.outbound_dashboard_hourly_queued),然后在视图不为 Null 时加载片段保持 if 检查。

public void loadFragment(Fragment fragment){
    FragmentManager fragmentManager = Objects.requireNonNull(getActivity()).getSupportFragmentManager();

    // Begin Fragment transaction.
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();// Replace the layout holder with the required Fragment object.
    View yourView= findViewById(R.id.outbound_dashboard_hourly_queued);
    if(yourView!=null){
    fragmentTransaction.replace(R.id.outbound_dashboard_hourly_queued, fragment);
    // Commit the Fragment replace action.
    fragmentTransaction.commit();
  }

}

【讨论】:

    【解决方案2】:

    试试这个

    检查是否添加了 Fragmnet

     if (getCurrentFragment() instanceof YourFragment)
    

    添加框架网

    replaceFragment(new HomeFragment(), "YourFragment");
    

    方法

        private void replaceFragment(Fragment fragment, String tag)
        {
            Fragment currentFragment = getCurrentFragment();
            if (currentFragment != null)
                getSupportFragmentManager().beginTransaction().remove(currentFragment).commit();
    
            getSupportFragmentManager().beginTransaction().add(R.id.container_main, fragment, tag).commit();
        }
    
        public Fragment getCurrentFragment()
        {
            FragmentManager manager = getSupportFragmentManager();
            return manager.findFragmentById(R.id.container_main);
        }
    
        private void popBackStack()
        {
            FragmentManager fragmentManager = getSupportFragmentManager();
            int total = fragmentManager.getBackStackEntryCount();
            for (int i = 0; i < total; i++)
            {
                fragmentManager.popBackStack();
            }
    
        }
    

    【讨论】:

      【解决方案3】:

      您可以使用 FragmentManager 提供的 findFragmentByTag(String tag) 方法来检查片段是否已经存在。

      Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG);

      为此,您需要使用该 TAG 提交片段 -

      fragmentTransaction.replace(R.id.outbound_dashboard_hourly_queued, fragment, TAG);

      【讨论】:

        【解决方案4】:

        如果计数== 0,请在替换前进行计数检查,否则替换您的代码必须是这样的

        public void loadFragment(Fragment fragment){
            FragmentManager fragmentManager = Objects.requireNonNull(getActivity()).getSupportFragmentManager();
        
            // Begin Fragment transaction.
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();// Replace the layout holder with the required Fragment object.
        
            if(fragmentManager.getFragments().size() == 0){
                fragmentTransaction.add(R.id.outbound_dashboard_hourly_queued, fragment).commit();
            }else {
                fragmentTransaction.replace(R.id.outbound_dashboard_hourly_queued, fragment).commit();
            }
            // Commit the Fragment replace action.
        
        }
        

        请尝试一下并告诉我。

        【讨论】:

          猜你喜欢
          • 2013-12-10
          • 2015-07-11
          • 1970-01-01
          • 2019-03-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-09
          • 1970-01-01
          相关资源
          最近更新 更多