Android work experience Fragment management

Keywords: Fragment Android

Fragment, or fragment for short, is proposed by Android 3.0 (API 11). In order to be compatible with the lower version, a set of Fragment API has been developed in support-v4 library, which is at least compatible with Android 1.6.

In the past, the support-v4 library was a jar package. Since version 24.2.0, the support-v4 library has been modularized into multiple jar packages, including: support fragment, support UI, support media compat, etc. This is to reduce the APK package size and introduce which module you need.

Fragment, as one of the most basic and important basic concepts of Android, often deals with it in development.

When we develop, we often load fragment, sometimes manage it, and what type of fragment is currently in the front, and how to call it.. activity needs to detect the activity trace of fragment, etc

public class MyFragmentManager {
    private static List<Fragment> task = new ArrayList<>();
    private static MyFragmentManager sFragmentManager;
    private onFragmentChangeListener mOnFragmentChangeListener;

    private MyFragmentManager() {
    }

    public static MyFragmentManager getInstance() {
        if (sFragmentManager == null) {
            sFragmentManager = new MyFragmentManager();
        }
        return sFragmentManager;
    }

    // This method is to add a fragment. It is better to add it in oncreateview of base
    public void pushOneFragment(Fragment f) {
        task.add(0, f);
        if (mOnFragmentChangeListener != null) {
            mOnFragmentChangeListener.onAdd();
        }
    }

    // When fragment is closed, this method is called for destruction
    public void PopOneFragment(Fragment f) {
        if (task.contains(f)) {
            task.remove(f);
            f = null;
            if (mOnFragmentChangeListener != null) {
                mOnFragmentChangeListener.onPop();
            }
        }
    }

    // Jump to a fragment
    public void PopToStartFragment() {
        for (int i = 0; i < task.size(); i++) {
            Fragment mySupportFragment = task.get(i);
          
            if (mySupportFragment instanceof Start) {
                break;
            }else {
                // Here is my own close method, which is the unified fragment destroy method
                mySupportFragment.pop();
            }
        }
    }

    // Get the top-level fragment
    public Fragment getTopFragment() {
        Fragment mySupportFragment = null;
        if (task.size() != 0) {
            mySupportFragment = task.get(0);
        }
        return mySupportFragment;
    }

    public void onViewCreated(Fragment mySupportFragment) {
        if (mOnViewCratedListener!= null) {
            mOnViewCratedListener.onViewCreated(mySupportFragment);
        }
    }

    private onViewCratedListener mOnViewCratedListener;

    public void setOnViewCratedListener(onViewCratedListener onViewCratedListener) {
        mOnViewCratedListener = onViewCratedListener;
    }

    public interface onViewCratedListener{
        void onViewCreated(Fragment mySupportFragment);
    }



    public onFragmentChangeListener getOnFragmentChangeListener() {
        return mOnFragmentChangeListener;
    }

    public void setOnFragmentChangeListener(onFragmentChangeListener onFragmentChangeListener) {
        mOnFragmentChangeListener = onFragmentChangeListener;
    }

    public interface onFragmentChangeListener{

        void onAdd();
        void onPop();

    }

 

}

 

Posted by ploppy on Sat, 28 Dec 2019 11:37:32 -0800