如果是只有一个Activity的App就没有必要写BaseActivity,但一旦大于一个我还是建议写一个BaseActivity,准没错。
每个Activity通常都是绑定视图,绑定控件,监听控件,获取Bundle,跳转Activity还有一些很有些烦人的小功能如:Toast,findViewById,我们都可以封装一层简化他们的使用
BaseActivity的代码如下:
public abstract class BaseActivity extends Activity implements View.OnClickListener {
/** 是否沉浸状态栏 **/
private boolean isSetStatusBar = true;
/** 是否允许全屏 **/
private boolean mAllowFullScreen = true;
/** 是否禁止旋转屏幕 **/
private boolean isAllowScreenRoate = false;
/** 当前Activity渲染的视图View **/
private View mContextView = null;
/** 日志输出标志 **/
protected final String TAG = this.getClass().getSimpleName();
/** View点击 **/
public abstract void widgetClick(View v);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "BaseActivity-->onCreate()");
Bundle bundle = getIntent().getExtras();
initParms(bundle);
View mView = bindView();
if (null == mView) {
mContextView = LayoutInflater.from(this)
.inflate(bindLayout(), null);
} else
mContextView = mView;
if (mAllowFullScreen) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
if (isSetStatusBar) {
steepStatusBar();
}
setContentView(mContextView);
if (!isAllowScreenRoate) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
initView(mContextView);
setListener();
doBusiness(this);
}
/**
* [沉浸状态栏]
*/
private void steepStatusBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// 透明状态栏
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// 透明导航栏
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
/**
* [初始化参数]
*
* @param parms
*/
public abstract void initParms(Bundle parms);
/**
* [绑定视图]
*
* @return
*/
public abstract View bindView();
/**
* [绑定布局]
*
* @return
*/
public abstract int bindLayout();
/**
* [初始化控件]
*
* @param view
*/
public abstract void initView(final View view);
/**
* [绑定控件]
*
* @param resId
*
* @return
*/
protected <T extends View> T $(int resId) {
return (T) super.findViewById(resId);
}
/**
* [设置监听]
*/
public abstract void setListener();
@Override
public void onClick(View v) {
widgetClick(v);
}
/**
* [业务操作]
*
* @param mContext
*/
public abstract void doBusiness(Context mContext);
/**
* [页面跳转]
*
* @param clz
*/
public void startActivity(Class<?> clz) {
startActivity(new Intent(BaseActivity.this,clz));
}
/**
* [携带数据的页面跳转]
*
* @param clz
* @param bundle
*/
public void startActivity(Class<?> clz, Bundle bundle) {
Intent intent = new Intent();
intent.setClass(this, clz);
if (bundle != null) {
intent.putExtras(bundle);
}
startActivity(intent);
}
/**
* [含有Bundle通过Class打开编辑界面]
*
* @param cls
* @param bundle
* @param requestCode
*/
public void startActivityForResult(Class<?> cls, Bundle bundle,
int requestCode) {
Intent intent = new Intent();
intent.setClass(this, cls);
if (bundle != null) {
intent.putExtras(bundle);
}
startActivityForResult(intent, requestCode);
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart()");
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart()");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume()");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy()");
}
/**
* [简化Toast]
* @param msg
*/
protected void showToast(String msg){
Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
}
/**
* [是否允许屏幕旋转]
*
* @param isAllowScreenRoate
*/
public void setScreenRoate(boolean isAllowScreenRoate) {
this.isAllowScreenRoate = isAllowScreenRoate;
}
}
/**
* [是否允许全屏]
*
* @param allowFullScreen
*/
public void setAllowFullScreen(boolean allowFullScreen) {
this.mAllowFullScreen = allowFullScreen;
}
/**
* [是否设置沉浸状态栏]
*
* @param isSetStatusBar
*/
public void setSteepStatusBar(boolean isSetStatusBar) {
this.isSetStatusBar = isSetStatusBar;
}
/**
* [是否允许屏幕旋转]
*
* @param isAllowScreenRoate
*/
public void setScreenRoate(boolean isAllowScreenRoate) {
this.isAllowScreenRoate = isAllowScreenRoate;
}
}[是否设置沉浸状态栏]
*
* @param isSetStatusBar
*/
public void setSteepStatusBar(boolean isSetStatusBar) {
this.isSetStatusBar = isSetStatusBar;
}
/**
* [是否允许屏幕旋转]
*
* @param isAllowScreenRoate
*/
public void setScreenRoate(boolean isAllowScreenRoate) {
this.isAllowScreenRoate = isAllowScreenRoate;
}
}
可以看到还是很多重复代码的,所以说搞一个BaseActivity是多么有必要
继承自BaseActivity的Activity使用如下:
public class MainActivity extends BaseActivity {
private Button btn1;
@Override
public void widgetClick(View v) {
switch (v.getId()){
case R.id.button:
//用简化了的toast和startActivity
showToast("toast");
startActivity(Main2Activity.class);
break;
}
}
@Override
public void initParms(Bundle parms) {
//解析bundle内容或者设置是否旋转,沉浸,全屏
}
@Override
public View bindView() {
return null;
}
@Override
public int bindLayout() {
return R.layout.activity_main;
}
@Override
public void initView(View view) {
btn1=$(R.id.button);
}
@Override
public void setListener() {
btn1.setOnClickListener(this);
}
@Override
public void doBusiness(Context mContext) {
}
}
其实不仅是Activity要搞一个基类,Fragment也要搞一个基类,这样可以大大减少重复代码,而且管理起来还比较容易,保持了样式的一致,如果想个性化也预留了相应的方法。