getSupportFragmentManager().getFragments() deprecated?

前端 未结 1 1577
太阳男子
太阳男子 2021-01-20 23:48

I am working with a viewpager and compiling API 23. I\'m showing a compile error in my code for the following statement, but the project does compile.

List<         


        
1条回答
  •  野的像风
    2021-01-21 00:48

    I actually did this to get a reference to all the fragments:

    private List> mFragList = new ArrayList>();
    
    @Override
    public void onAttachFragment (Fragment fragment) {
        mFragList.add(new WeakReference(fragment));
    }
    
    public List getActiveFragments() {
        ArrayList ret = new ArrayList();
        for(WeakReference ref : mFragList) {
            Fragment f = ref.get();
            if(f != null) {
                if(f.isVisible()) {
                    ret.add(f);
                }
            }
        }
        return ret;
    }
    
    public Fragment findFragement(String tClass) {
    
        List fragments = getActiveFragments();
        for (Fragment fragment : fragments) {
            if (tClass.equalsIgnoreCase("Home")) {
                if (fragment instanceof ToggleFragment) {
                    return fragment;
    
                }
            } else if (tClass.equalsIgnoreCase("Contacts")) {
                if (fragment instanceof ContactFragment) {
                    return fragment;
                }
            }
        }
        return null;
    }
    

    0 讨论(0)
提交回复
热议问题