1. 程式人生 > >封裝篇——基類Activity

封裝篇——基類Activity

說說為什麼要做基類Activity:
1. 統一風格,易調整(吐司、系統欄顏色、進入退出動畫…)
2. 細化基礎生命週期方法,減少程式碼量。
總而言之,封裝就是為了更優雅的寫程式碼。。。

本文將從以下思路展開:
這裡寫圖片描述

先要理清楚下Android中 Activity 的一些知識點

  • android Activity 生命週期詳解 (在 執行Activity、按下返回按鍵、按Home鍵、
    按下電源鍵、跳轉新Activity、再次開啟、螢幕旋轉 等場景下Activity的生命週期走向);這裡寫圖片描述
  • 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();
/**
 * 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();
        }
    }
}

實際呼叫請大家自行實驗,,請大家不要過分看重程式碼實現,思路最重要…