说说为什么要做基类Activity:
1. 统一风格,易调整(吐司、系统栏颜色、进入退出动画…)
2. 细化基础生命周期方法,减少代码量。
总而言之,封装就是为了更优雅的写代码。。。
先要理清楚下Android中 Activity 的一些知识点:
了解完知识点后我们就可以进行简单的封装了
我们先创建一个 BaseAppCompatActivity
类对生命周期进行抽离打包,该类与业务完全无关
看图中思路就行,代码很简单。
/**
* Created by wenld- on 2016/4/6.
*/
public abstract class BaseAppCompatActivity extends AppCompatActivity {
protected static String TAG_LOG = null;
protected Context mContext = null;
public static final int LEFT = 1, RIGHT = 2, TOP = 3, BOTTOM = 4, SCALE = 5, FADE = 6;
@Override
protected void onCreate(Bundle savedInstanceState) {
switch (getOverridePendingTransitionMode()) {
case LEFT:
overridePendingTransition(R.anim.left_in, R.anim.left_out);
break;
case RIGHT:
overridePendingTransition(R.anim.right_in, R.anim.right_out);
break;
case TOP:
overridePendingTransition(R.anim.top_in, R.anim.top_out);
break;
case BOTTOM:
overridePendingTransition(R.anim.bottom_in, R.anim.bottom_out);
break;
case SCALE:
overridePendingTransition(R.anim.scale_in, R.anim.scale_out);
break;
case FADE:
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
break;
default:
overridePendingTransition(R.anim.left_in, R.anim.left_out);
break;
}
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (null != extras) {
getBundleExtras(extras);
}
mContext = this;
TAG_LOG = this.getClass().getSimpleName();
BaseAppManager.getInstance().addActivity(this);
if (getContentViewLayoutID() != 0) {
setContentView(getContentViewLayoutID());
} else {
throw new IllegalArgumentException("You must return a right contentView layout resource Id");
}
initViewsAndEvents();
}
@Override
public void finish() {
super.finish();
BaseAppManager.getInstance().removeActivity(this);
switch (getOverridePendingTransitionMode()) {
case LEFT:
overridePendingTransition(R.anim.left_in, R.anim.left_out);
break;
case RIGHT:
overridePendingTransition(R.anim.right_in, R.anim.right_out);
break;
case TOP:
overridePendingTransition(R.anim.top_in, R.anim.top_out);
break;
case BOTTOM:
overridePendingTransition(R.anim.bottom_in, R.anim.bottom_out);
break;
case SCALE:
overridePendingTransition(R.anim.scale_in, R.anim.scale_out);
break;
case FADE:
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
break;
default:
overridePendingTransition(R.anim.left_in, R.anim.left_out);
break;
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// 当新设置中,屏幕布局模式为横排时
if (newConfig.orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
//TODO 某些操作
}
super.onConfigurationChanged(newConfig);
}
@Override
protected void onDestroy() {
super.onDestroy();
DetoryViewAndThing();
}
protected abstract int getContentViewLayoutID();
protected abstract void getBundleExtras(Bundle extras);
protected abstract void initViewsAndEvents();
protected abstract int getOverridePendingTransitionMode();
protected abstract void DetoryViewAndThing();
接下来我们创建一个class BaseActivity extends BaseAppCompatActivity
,对风格,方法,以及基础页面的封装,可参考:http://blog.csdn.net/sinat_15877283/article/details/51037987 内高级方法。
/**
* Created by wenld- on 2016/3/7.
*/
public abstract class BaseActivity extends BaseAppCompatActivity implements IBaseView {
/**
* loading view controller
*/
private VaryViewHelperController mVaryViewHelperController = null;
@Override
protected int getOverridePendingTransitionMode() {
return LEFT;
}
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
if (null != getLoadingTargetView()) {
mVaryViewHelperController = new VaryViewHelperController(getLoadingTargetView());
}
setStatusBar();
}
protected void setStatusBar() {
StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimary));
}
/**
* get loading target view
*/
protected abstract View getLoadingTargetView();
protected void readyGo(Class> clazz) {
Intent intent = new Intent(this, clazz);
startActivity(intent);
}
protected void readyGo(Class> clazz, Bundle bundle) {
Intent intent = new Intent(this, clazz);
if (null != bundle) {
intent.putExtras(bundle);
}
startActivity(intent);
}
protected void readyGoThenKill(Class> clazz) {
Intent intent = new Intent(this, clazz);
startActivity(intent);
finish();
}
protected void readyGoThenKill(Class> clazz, Bundle bundle) {
Intent intent = new Intent(this, clazz);
if (null != bundle) {
intent.putExtras(bundle);
}
startActivity(intent);
finish();
}
protected void readyGoForResult(Class> clazz, int requestCode) {
Intent intent = new Intent(this, clazz);
startActivityForResult(intent, requestCode);
}
protected void readyGoForResult(Class> clazz, int requestCode, Bundle bundle) {
Intent intent = new Intent(this, clazz);
if (null != bundle) {
intent.putExtras(bundle);
}
startActivityForResult(intent, requestCode);
}
protected void showToast(String msg) {
//防止遮盖虚拟按键
if (null != msg && !StringUtils.isEmpty(msg)) {
Snackbar.make(getLoadingTargetView(), msg, Snackbar.LENGTH_SHORT).show();
}
}
/**
* toggle show loading
*/
protected void toggleShowLoading(boolean toggle, String msg) {
if (null == mVaryViewHelperController) {
throw new IllegalArgumentException("You must return a right target view for loading");
}
if (toggle) {
mVaryViewHelperController.showLoading(msg);
} else {
mVaryViewHelperController.restore();
}
}
/**
* toggle show empty
*/
protected void toggleShowEmpty(boolean toggle, String msg, View.OnClickListener onClickListener) {
if (null == mVaryViewHelperController) {
throw new IllegalArgumentException("You must return a right target view for loading");
}
if (toggle) {
mVaryViewHelperController.showEmpty(msg, onClickListener);
} else {
mVaryViewHelperController.restore();
}
}
/**
* toggle show error
*/
protected void toggleShowError(boolean toggle, String msg, View.OnClickListener onClickListener) {
if (null == mVaryViewHelperController) {
throw new IllegalArgumentException("You must return a right target view for loading");
}
if (toggle) {
mVaryViewHelperController.showError(msg, onClickListener);
} else {
mVaryViewHelperController.restore();
}
}
/**
* toggle show network error
*/
protected void toggleNetworkError(boolean toggle, View.OnClickListener onClickListener) {
if (null == mVaryViewHelperController) {
throw new IllegalArgumentException("You must return a right target view for loading");
}
if (toggle) {
mVaryViewHelperController.showNetworkError(onClickListener);
} else {
mVaryViewHelperController.restore();
}
}
}
实际调用请大家自行实验,,请大家不要过分看重代码实现,思路最重要…