| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- package com.hjq.base;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Looper;
- import android.os.SystemClock;
- import android.support.annotation.Nullable;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.view.inputmethod.InputMethodManager;
- import java.util.Random;
- /**
- * desc : Activity 基类
- */
- public abstract class BaseActivity extends AppCompatActivity {
- private static final Handler HANDLER = new Handler(Looper.getMainLooper());
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- initActivity();
- }
- protected void initActivity() {
- initLayout();
- initView();
- initData();
- }
- protected void initLayout() {
- if (getLayoutId() > 0) {
- setContentView(getLayoutId());
- }
- }
- // 引入布局
- protected abstract int getLayoutId();
- // 标题栏
- protected abstract int getTitleId();
- // 初始化控件
- protected abstract void initView();
- // 初始化数据
- protected abstract void initData();
- @Override
- public void finish() {
- hideSoftKeyboard();
- super.finish();
- }
- /**
- * 延迟执行
- */
- public final boolean post(Runnable r) {
- return postDelayed(r, 0);
- }
- /**
- * 延迟一段时间执行
- */
- public final boolean postDelayed(Runnable r, long delayMillis) {
- if (delayMillis < 0) {
- delayMillis = 0;
- }
- return postAtTime(r, SystemClock.uptimeMillis() + delayMillis);
- }
- /**
- * 在指定的时间执行
- */
- public final boolean postAtTime(Runnable r, long uptimeMillis) {
- return HANDLER.postAtTime(r, this, uptimeMillis);
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- // 移除和这个 Activity 相关的消息回调
- HANDLER.removeCallbacksAndMessages(this);
- }
- /**
- * 如果当前的 Activity(singleTop 启动模式) 被复用时会回调
- */
- @Override
- protected void onNewIntent(Intent intent) {
- super.onNewIntent(intent);
- // 设置为当前的 Intent,避免 Activity 被杀死后重启 Intent 还是最原先的那个
- setIntent(intent);
- }
- /**
- * 获取当前 Activity 对象
- */
- @SuppressWarnings("unchecked")
- public <A extends BaseActivity> A getActivity() {
- return (A) this;
- }
- /**
- * 和 setContentView 对应的方法
- */
- public View getContentView() {
- return getWindow().getDecorView();
- }
- /**
- * startActivity 方法优化
- */
- public void startActivity(Class<? extends Activity> cls) {
- startActivity(new Intent(this, cls));
- }
- public void startActivityFinish(Class<? extends Activity> cls) {
- startActivityFinish(new Intent(this, cls));
- }
- public void startActivityFinish(Intent intent) {
- startActivity(intent);
- finish();
- }
- /**
- * setResult 方法优化
- */
- public void finishResult() {
- finishResult(RESULT_OK, null);
- }
- public void finishResult(int resultCode) {
- finishResult(resultCode, null);
- }
- public void finishResult(int resultCode, Intent data) {
- setResult(resultCode, data);
- finish();
- }
- /**
- * startActivityForResult 方法优化
- */
- private ActivityCallback mActivityCallback;
- private int mActivityRequestCode;
- public void startActivityForResult(Class<? extends Activity> cls, ActivityCallback callback) {
- startActivityForResult(new Intent(this, cls), null, callback);
- }
- public void startActivityForResult(Intent intent, ActivityCallback callback) {
- startActivityForResult(intent, null, callback);
- }
- public void startActivityForResult(Intent intent, @Nullable Bundle options, ActivityCallback callback) {
- if (mActivityCallback == null) {
- mActivityCallback = callback;
- // 随机生成请求码,这个请求码在 0 - 255 之间
- mActivityRequestCode = new Random().nextInt(255);
- startActivityForResult(intent, mActivityRequestCode, options);
- }else {
- // 回调还没有结束,所以不能再次调用此方法,这个方法只适合一对一回调,其他需求请使用原生的方法实现
- throw new IllegalArgumentException("Error, The callback is not over yet");
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
- if (mActivityCallback != null && mActivityRequestCode == requestCode) {
- mActivityCallback.onActivityResult(resultCode, data);
- mActivityCallback = null;
- }else {
- super.onActivityResult(requestCode, resultCode, data);
- }
- }
- /**
- * 处理 Activity 多重跳转:https://www.jianshu.com/p/579f1f118161
- */
- @Override
- public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options) {
- if (startActivitySelfCheck(intent)) {
- hideSoftKeyboard();
- // 查看源码得知 startActivity 最终也会调用 startActivityForResult
- super.startActivityForResult(intent, requestCode, options);
- }
- }
- private String mStartActivityTag;
- private long mStartActivityTime;
- /**
- * 检查当前 Activity 是否重复跳转了,不需要检查则重写此方法并返回 true 即可
- *
- * @param intent 用于跳转的 Intent 对象
- * @return 检查通过返回true, 检查不通过返回false
- */
- protected boolean startActivitySelfCheck(Intent intent) {
- // 默认检查通过
- boolean result = true;
- // 标记对象
- String tag;
- if (intent.getComponent() != null) { // 显式跳转
- tag = intent.getComponent().getClassName();
- }else if (intent.getAction() != null) { // 隐式跳转
- tag = intent.getAction();
- }else { // 其他方式
- return true;
- }
- if (tag.equals(mStartActivityTag) && mStartActivityTime >= SystemClock.uptimeMillis() - 500) {
- // 检查不通过
- result = false;
- }
- mStartActivityTag = tag;
- mStartActivityTime = SystemClock.uptimeMillis();
- return result;
- }
- /**
- * 隐藏软键盘
- */
- private void hideSoftKeyboard() {
- // 隐藏软键盘,避免软键盘引发的内存泄露
- View view = getCurrentFocus();
- if (view != null) {
- InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
- if (manager != null) manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
- }
- }
- /**
- * Activity 回调接口
- */
- public interface ActivityCallback {
- /**
- * 结果回调
- *
- * @param resultCode 结果码
- * @param data 数据
- */
- void onActivityResult(int resultCode, @Nullable Intent data);
- }
- }
|