Java tutorial
/* * Copyright (c) 2015 DreamLiner Studio * Licensed under the Apache License, Version 2.0 (the "License?); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.chenzhongjin.realmsample.ui.base; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.text.TextUtils; import android.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import org.greenrobot.eventbus.EventBus; import java.lang.reflect.Field; /** * @author chenzj * @Title: BaseFragment * @Description: ?? - baseFragment.?Eventbus.Log.sweetDialog * @date 2015-6-28 * @email chenzhongjin@vip.qq.com */ public abstract class BaseLazyFragment extends Fragment { /** * Screen information */ protected int mScreenWidth = 0; protected int mScreenHeight = 0; protected float mScreenDensity = 0.0f; /** * context */ public Activity mContext = null; private boolean isFirstResume = true; private boolean isFirstVisible = true; private boolean isFirstInvisible = true; private boolean isPrepared; /** * bind layout resource file * * @return id of layout resource */ protected abstract int getLayoutId(); protected boolean isRegisterEvent() { return false; } /** * init all views and add events */ protected abstract void initViews(View view); /** * sometime will extent BaseLazyFra to init some specialview.such as recyclerview/head */ protected void initSpecialView(View view) { } /** * when fragment is visible for the first time, here we can do some initialized work or refresh data only once */ protected void onFirstUserVisible() { } /** * this method like the fragment's lifecycle method onResume() */ protected void onUserVisible() { } /** * when fragment is invisible for the first time */ private void onFirstUserInvisible() { // here we do not recommend do something } /** * this method like the fragment's lifecycle method onPause() */ protected void onUserInvisible() { } @Override public void onAttach(Activity activity) { super.onAttach(activity); mContext = activity; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (isRegisterEvent()) { EventBus.getDefault().register(this); } } @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { if (getLayoutId() != 0) { return inflater.inflate(getLayoutId(), null); } else { return super.onCreateView(inflater, container, savedInstanceState); } } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); DisplayMetrics displayMetrics = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); mScreenDensity = displayMetrics.density; mScreenHeight = displayMetrics.heightPixels; mScreenWidth = displayMetrics.widthPixels; initSpecialView(view); initViews(view); } @Override public void onDestroyView() { super.onDestroyView(); } @Override public void onDestroy() { if (isRegisterEvent()) { EventBus.getDefault().unregister(this); } super.onDestroy(); } @Override public void onDetach() { super.onDetach(); // for bug ---> java.lang.IllegalStateException: Activity has been destroyed try { Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager"); childFragmentManager.setAccessible(true); childFragmentManager.set(this, null); } catch (NoSuchFieldException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); initPrepare(); } @Override public void onResume() { super.onResume(); if (isFirstResume) { isFirstResume = false; return; } if (getUserVisibleHint()) { onUserVisible(); } } @Override public void onPause() { super.onPause(); if (getUserVisibleHint()) { onUserInvisible(); } } @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser) { if (isFirstVisible) { isFirstVisible = false; initPrepare(); } else { onUserVisible(); } } else { if (isFirstInvisible) { isFirstInvisible = false; onFirstUserInvisible(); } else { onUserInvisible(); } } } private synchronized void initPrepare() { if (isPrepared) { onFirstUserVisible(); } else { isPrepared = true; } } public BaseCompatActivity getBaseAct() { return (BaseCompatActivity) mContext; } /** * Eventbus * * @param event */ public void postEvent(Object event) { EventBus.getDefault().post(event); } /** * get the support fragment manager * * @return */ protected FragmentManager getSupportFragmentManager() { return getActivity().getSupportFragmentManager(); } /** * startActivity * * @param clazz */ protected void readyGo(Class<?> clazz) { Intent intent = new Intent(getActivity(), clazz); startActivity(intent); } /** * startActivity with bundle * * @param clazz * @param bundle */ protected void readyGo(Class<?> clazz, Bundle bundle) { Intent intent = new Intent(getActivity(), clazz); if (null != bundle) { intent.putExtras(bundle); } startActivity(intent); } /** * startActivityForResult * * @param clazz * @param requestCode */ protected void readyGoForResult(Class<?> clazz, int requestCode) { Intent intent = new Intent(getActivity(), clazz); startActivityForResult(intent, requestCode); } /** * startActivityForResult with bundle * * @param clazz * @param requestCode * @param bundle */ protected void readyGoForResult(Class<?> clazz, int requestCode, Bundle bundle) { Intent intent = new Intent(getActivity(), clazz); if (null != bundle) { intent.putExtras(bundle); } startActivityForResult(intent, requestCode); } /** * show toast * * @param msg */ protected void showToast(String msg) { if (null != msg && !TextUtils.isEmpty(msg)) { if (mContext instanceof BaseCompatActivity) { ((BaseCompatActivity) mContext).showToast(msg); } } } protected void showToast(int resId) { if (mContext instanceof BaseCompatActivity) { ((BaseCompatActivity) mContext).showToast(resId); } } public BaseCompatActivity.MyHandler getHandler() { return ((BaseCompatActivity) mContext).mHandler; } }