美文网首页
基类Activity

基类Activity

作者: palas猫 | 来源:发表于2017-05-05 10:47 被阅读0次

    1:包含功能

    Activity统一管理

    沉浸式菜单

    EventBus注册和解除

    Activity跳转方法抽取

    返回键监听方法抽取

    2:代码

    public abstract classMActivityextendsActivity {

    @Override

    protected voidonCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    MActivityManager.getInstance().pushActivity(this);

    setTheme(R.style.TranslucentTheme);//沉浸式

    requestWindowFeature(Window.FEATURE_NO_TITLE);//沉浸式

    EventBus.getDefault().register(this);

    setContentView(initContentView());

    initState();//沉浸式

    initView();

    initData();

    initEvent();

    }

    protected abstract intinitContentView();

    protected abstract voidinitView();

    protected abstract voidinitData();

    protected abstract voidinitEvent();

    @Subscribe

    public voidonReceive(Object o){

    }

    EventBus的事件接收方法,子类选择性复写

    /**

    *@time2017/4/14 9:40

    *@desc跳转到activity

    */

    protected voidtoActivity(Class c,booleanclearOther,HashMap map) {

    IntentUtils.makeIntent(this,c,clearOther,map);

    }

    /**

    *@time2017/4/14 9:40

    *@desc跳转到activity

    */

    protected voidtoActivity(Class c,HashMap map,booleanisFinish) {

    IntentUtils.makeIntent(this,c,isFinish,map);

    }

    /**

    *@time2017/4/14 9:35

    *@desc返回键监听事件

    */

    protected booleanbackPress() {

    super.onBackPressed();

    return false;

    }

    /**

    *@time2017/4/1 18:02

    *@desc沉浸式

    */

    private voidinitState() {

    ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);

    View parentView = contentFrameLayout.getChildAt(0);

    if(parentView !=null&& Build.VERSION.SDK_INT>=14) {

    parentView.setFitsSystemWindows(true);

    }

    StatusBarCompat.compat(this,getResources().getColor(R.color.base_red));

    }

    @Override

    protected voidonResume() {

    super.onResume();

    }

    @Override

    protected voidonDestroy() {

    super.onDestroy();

    EventBus.getDefault().unregister(this);

    MActivityManager.getInstance().popActivity(this);

    }

    @Override

    public booleanonKeyDown(intkeyCode,KeyEvent event) {

    if((keyCode == KeyEvent.KEYCODE_BACK)) {

    returnbackPress();

    }else{

    return super.onKeyDown(keyCode,event);

    }

    }

    @Override

    public voidonBackPressed() {

    backPress();

    }

    }

    3:用到的类MActivityManager

    public class MActivityManager{

    private staticMActivityManagerMActivityManager;

    private staticStackactivityStack;

    privateMActivityManager() {

    }

    //退出棧頂Activity

    public voidpopActivity(Activity activity) {

    if(activity !=null) {

    //在從自定義集合中取出當前Activity時,也進行了Activity的關閉操作

    activity.finish();

    activityStack.remove(activity);

    activity =null;

    }

    }

    //獲得當前棧頂Activity

    publicActivitycurrentActivity() {

    Activity activity =null;

    if(!activityStack.empty())

    activity=activityStack.lastElement();

    returnactivity;

    }

    //將當前Activity推入棧中

    public voidpushActivity(Activity activity) {

    if(activityStack==null) {

    activityStack=newStack();

    }

    activityStack.add(activity);

    }

    //退出棧中所有Activity

    public voidpopAllActivity() {

    while(true) {

    Activity activity = currentActivity();

    if(activity ==null) {

    break;

    }

    popActivity(activity);

    }

    }

    //退出棧中所有Activity

    public voidpopAllActivityExceptOne(Class cls) {

    while(true) {

    Activity activity = currentActivity();

    if(activity ==null) {

    break;

    }

    if(activity.getClass().equals(cls)) {

    break;

    }

    popActivity(activity);

    }

    }

    //通过一个单例模式返回当前类的实例对象

    public staticMActivityManagergetInstance() {

    if(MActivityManager==null) {

    synchronized(MActivityManager.class) {

    if(MActivityManager==null) {

    MActivityManager=newMActivityManager();

    }

    }

    }

    returnMActivityManager;

    }

    }

    4:用到的类IntentUtils

    public class IntentUtils{

    /**

    *@time2017/4/17 16:56

    *@descintent参数,支持String,Integer,Double,Folat

    */

    private static voidaddParam(Intent intent,HashMap map) {

    for(String s : map.keySet()) {

    Object o = map.get(s);

    if(oinstanceofString) {

    intent.putExtra(s,(String) map.get(s));

    }else if(oinstanceofInteger) {

    intent.putExtra(s,(Integer) map.get(s));

    }else if(oinstanceofDouble) {

    intent.putExtra(s,(Double) map.get(s));

    }else if(oinstanceofFloat) {

    intent.putExtra(s,(Float) map.get(s));

    }else if(oinstanceofSerializable) {

    intent.putExtra(s,(Serializable) map.get(s));

    }else{

    LogUtils.i("MActivity_toActivityClearOther","暂不支持的参数类型");

    }

    }

    }

    public static voidmakeIntent(Context context,Class c, booleanclearOther,HashMap map) {

    if(clearOther) {

    MActivityManager.getInstance().popAllActivityExceptOne(context.getClass());

    }

    Intent intent =newIntent(context,c);

    if(map !=null) {

    addParam(intent,map);

    }

    context.startActivity(intent);

    if(clearOther) {

    MActivityManager.getInstance().popAllActivityExceptOne(c);

    }

    }

    public static voidmakeIntent(Context context,Class c,HashMap map,booleanisFinish){

    Intent intent =newIntent(context,c);

    if(map !=null) {

    addParam(intent,map);

    }

    context.startActivity(intent);

    if(isFinish){

    try{

    ((Activity)context).finish();

    }catch(Exception e){

    LogUtils.e("IntentUtils_makeIntent","Context不是Activity类型");

    }

    }

    }

    }

    5:用到的主题

    相关文章

      网友评论

          本文标题:基类Activity

          本文链接:https://www.haomeiwen.com/subject/epmjtxtx.html