SHARE
TWEET

Untitled

a guest Jan 12th, 2017 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * 在内存重启的时候自动恢复可见状态的Fragment,避免重叠现象
  3.  * 但同时不要忘了在Activity中判断`savedInstanceState`为null的时候才操作Fragment
  4.  * Created by chensuilun on 16-8-9.
  5.  */
  6. public abstract class BaseRestoreFragment extends Fragment {
  7.     public static final String IS_SHOW = "is_show";
  8.     protected View mRootView;
  9.  
  10.     @Override
  11.     public void onCreate(@Nullable Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         if (BuildConfig.DEBUG) {
  14.             Log.d(getClass().getSimpleName(), "onCreate:");
  15.         }
  16.         if (savedInstanceState != null && savedInstanceState.getBoolean(IS_SHOW) && getFragmentManager() != null) {
  17.             getFragmentManager().beginTransaction().show(this).commit();
  18.         }
  19.     }
  20.  
  21.     @Override
  22.     public void onSaveInstanceState(Bundle outState) {
  23.         super.onSaveInstanceState(outState);
  24.         outState.putBoolean(IS_SHOW, !isHidden());
  25.     }
  26.  
  27.     @Nullable
  28.     @Override
  29.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  30.         if (BuildConfig.DEBUG) {
  31.             Log.d(getClass().getSimpleName(), "onCreateView:");
  32.         }
  33.         mRootView = onCreateContentView(inflater, container, savedInstanceState);
  34.         return mRootView;
  35.     }
  36.  
  37.  
  38.     @Override
  39.     public void onViewCreated(View view, Bundle savedInstanceState) {
  40.         super.onViewCreated(view, savedInstanceState);
  41.         if (BuildConfig.DEBUG) {
  42.             Log.v(getClass().getSimpleName(), "onViewCreated: ");
  43.         }
  44.         initView(view);
  45.         initData();
  46.     }
  47.  
  48.     @Override
  49.     public void onStop() {
  50.         super.onStop();
  51.         if (BuildConfig.DEBUG) {
  52.             Log.d(getClass().getSimpleName(), "onStop: ");
  53.         }
  54.     }
  55.  
  56.     @Override
  57.     public void onDestroyView() {
  58.         super.onDestroyView();
  59.         if (BuildConfig.DEBUG) {
  60.             Log.d(getClass().getSimpleName(), "onDestroyView ");
  61.         }
  62.     }
  63.  
  64.     @Override
  65.     public void onDestroy() {
  66.         super.onDestroy();
  67.         if (BuildConfig.DEBUG) {
  68.             Log.d(getClass().getSimpleName(), "onDestroy: ");
  69.         }
  70.     }
  71.  
  72.     protected abstract View onCreateContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);
  73.  
  74.     protected abstract void initView(View root);
  75.  
  76.     protected abstract void initData();
  77.  
  78.  
  79. }
RAW Paste Data
Top