论如何写出优雅的Android代码--------BaseActivity

从名字就可以看出BaseActivity是Activity的基类,这个基类实现了大部分Activity的需要实现的方法,其中就包括把Activity添加/删除进sActivityStack中,还包括Activity之间的跳转,提示框的展示等等。看代码吧

public class BaseActivity extends FragmentActivity {

	ProgressDialog mProgressDialog;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		ActivityManager.getInstance().push(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		ActivityManager.getInstance().pop(this);
	}

	public void openActivity(Class<?> clazz) {
		openActivity(clazz, null);
	}

	public void openActivity(Class<?> clazz, Bundle bundle) {
		Intent intent = new Intent();
		intent.setClass(this, clazz);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		startActivity(intent);

	}

	protected void cancleProgressDialog() {
		if (mProgressDialog != null && !mProgressDialog.isShowing()) {
			mProgressDialog.cancel();
		}
	}

	protected void showLoadingDialog() {
		if (mProgressDialog != null) {
			mProgressDialog.show();
		} else {
			mProgressDialog = createProgressDialog();
			mProgressDialog.show();
		}

	}

	protected ProgressDialog createProgressDialog() {
		return null;
	}

	protected void showToast(int resId, boolean length) {
		Toast.makeText(this, resId,
				length ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT).show();
	}

	/**
	 * 
	 * @param msg
	 *            内容
	 * @param length
	 *            true为长时间,false为短时间
	 * @return: void
	 */
	protected void showToast(String msg, boolean length) {
		Toast.makeText(this, msg,
				length ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT).show();
	}
}
反正就是Activity的基类,继承了之后一定会使代码变得很优雅。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值