Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 在内存重启的时候自动恢复可见状态的Fragment,避免重叠现象
- * 但同时不要忘了在Activity中判断`savedInstanceState`为null的时候才操作Fragment
- * Created by chensuilun on 16-8-9.
- */
- public abstract class BaseRestoreFragment extends Fragment {
- public static final String IS_SHOW = "is_show";
- protected View mRootView;
- @Override
- public void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- if (BuildConfig.DEBUG) {
- Log.d(getClass().getSimpleName(), "onCreate:");
- }
- if (savedInstanceState != null && savedInstanceState.getBoolean(IS_SHOW) && getFragmentManager() != null) {
- getFragmentManager().beginTransaction().show(this).commit();
- }
- }
- @Override
- public void onSaveInstanceState(Bundle outState) {
- super.onSaveInstanceState(outState);
- outState.putBoolean(IS_SHOW, !isHidden());
- }
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- if (BuildConfig.DEBUG) {
- Log.d(getClass().getSimpleName(), "onCreateView:");
- }
- mRootView = onCreateContentView(inflater, container, savedInstanceState);
- return mRootView;
- }
- @Override
- public void onViewCreated(View view, Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- if (BuildConfig.DEBUG) {
- Log.v(getClass().getSimpleName(), "onViewCreated: ");
- }
- initView(view);
- initData();
- }
- @Override
- public void onStop() {
- super.onStop();
- if (BuildConfig.DEBUG) {
- Log.d(getClass().getSimpleName(), "onStop: ");
- }
- }
- @Override
- public void onDestroyView() {
- super.onDestroyView();
- if (BuildConfig.DEBUG) {
- Log.d(getClass().getSimpleName(), "onDestroyView ");
- }
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- if (BuildConfig.DEBUG) {
- Log.d(getClass().getSimpleName(), "onDestroy: ");
- }
- }
- protected abstract View onCreateContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);
- protected abstract void initView(View root);
- protected abstract void initData();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement