content: package my.project.silisili.main.base;
  
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  import androidx.fragment.app.Fragment;
  
  public abstract class LazyFragment extends Fragment {
      private boolean isFragmentVisible;
  
      private boolean isPrepared;
  
      private boolean isFirstLoad = true;
  
      private boolean forceLoad = false;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          isFirstLoad = true;
          View view = initViews(inflater, container, savedInstanceState);
          isPrepared = true;
          lazyLoad();
          return view;
      }
  
      @Override
      public void setUserVisibleHint(boolean isVisibleToUser) {
          super.setUserVisibleHint(isVisibleToUser);
          if (getUserVisibleHint()) {
              onVisible();
          } else {
              onInvisible();
          }
      }
  
      @Override
      public void onHiddenChanged(boolean hidden) {
          super.onHiddenChanged(hidden);
          if (!hidden) {
              onVisible();
          } else {
              onInvisible();
          }
      }
  
      protected void onVisible() {
          isFragmentVisible = true;
          lazyLoad();
      }
  
      protected void onInvisible() {
          isFragmentVisible = false;
      }
  
      protected void lazyLoad() {
          if (isPrepared() && isFragmentVisible()) {
              if (forceLoad || isFirstLoad()) {
                  forceLoad = false;
                  isFirstLoad = false;
                  initData();
              }
          }
      }
  
      @Override
      public void onDestroyView() {
          super.onDestroyView();
          isPrepared = false;
      }
  
      protected abstract View initViews(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);
  
      protected abstract void initData();
  
      public boolean isPrepared() {
          return isPrepared;
      }
  
  
      public boolean isFirstLoad() {
          return isFirstLoad;
      }
  
      public boolean isFragmentVisible() {
          return isFragmentVisible;
      }
  }
  