BaseActivity的抽取

继承性

一:定义
【1】在现实世界中就是子承父业
【2】在面向对象的世界当自己当中,继承就是一个类得到了另一个类当中的成员变量和成员方法
注:

  • 在Java当中只支持单继承,不允许多继承,
  • 子类不能继承父类中访问权限为private的成员变量和方法,
  • 子类可以重写父类的方法,以及命名与父类同名的成员变量
  • 当继承父类中的时候,如果父类中有抽象方法,就会重写父类中的抽象方法
  • 不会继承父类的构造函数,在子类的构造函数当中,必须调用父类的构造函数

二:函数的复写(override)
1:定义:
【1】:在具有父子关系的两个类当中;
【2】:父类和子类各有一个函数,这两个函数的定义(返回值类型,函数名和参数列表)完全相同
子类继承父类的函数,有的需要更改的过程
【3】如果子类复写了父类的方法,则运行是系统调用子类的方法,如果子类继承了父类的方法(为重写),则运行是调用父类的方法

在BaseActivity中一般抽取和封装哪些东西?
常用对话框、状态栏的颜色改变、后退键、键盘的显示与隐藏、生命周期方法(管理activity)、事件分发、find控件、fragment,广播等等

public class TCBaseActivity extends AppCompatActivity implements DialogControl {

    private static final String TAG = TCBaseActivity.class.getSimpleName();
    private boolean _isVisible;

    private ProgressDialog _waitDialog;

    //错误消息弹窗
    private ErrorDialogFragment mErrDlgFragment;


    //被踢下线广播监听
    private LocalBroadcastManager mLocalBroadcatManager;
    private BroadcastReceiver mExitBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mLocalBroadcatManager = LocalBroadcastManager.getInstance(this);
        mExitBroadcastReceiver = new ExitBroadcastRecevier();
        mLocalBroadcatManager.registerReceiver(mExitBroadcastReceiver, new IntentFilter(TCConstants.EXIT_APP));

        mErrDlgFragment = new ErrorDialogFragment();

        _isVisible = true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLocalBroadcatManager.unregisterReceiver(mExitBroadcastReceiver);
    }

    public void onReceiveExitMsg() {
        TCUtils.showKickOutDialog(this);
    }

    @Override
    public ProgressDialog showWaitDialog() {
        return showWaitDialog(R.string.loading);
    }

    @Override
    public ProgressDialog showWaitDialog(int resid) {
        return showWaitDialog(getString(resid));
    }

    @Override
    public ProgressDialog showWaitDialog(String message) {
        if (_isVisible) {
            if (_waitDialog == null) {
                _waitDialog = DialogHelp.getWaitDialogNotCancel(this, message);
            }
            if (_waitDialog != null) {
                _waitDialog.setMessage(message);
                _waitDialog.show();
            }
            return _waitDialog;
        }
        return null;
    }

    @Override
    public void hideWaitDialog() {
        if (_isVisible && _waitDialog != null) {
            try {
                _waitDialog.dismiss();
                _waitDialog = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public class ExitBroadcastRecevier extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(TCConstants.EXIT_APP)) {
                //在被踢下线的情况下,执行退出前的处理操作:停止推流、关闭群组
                onReceiveExitMsg();
            }
        }
    }

    protected void showErrorAndQuit(String errorMsg) {

        if (!mErrDlgFragment.isAdded() && !this.isFinishing()) {
            Bundle args = new Bundle();
            args.putString("errorMsg", errorMsg);
            mErrDlgFragment.setArguments(args);
            mErrDlgFragment.setCancelable(false);

            //此处不使用用.show(...)的方式加载dialogfragment,避免IllegalStateException
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.add(mErrDlgFragment, "loading");
            transaction.commitAllowingStateLoss();
        }
    }
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }

    public void showToast(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
    }
}

dialogControl接口的内容是关于对话框的

public interface DialogControl {

    public abstract void hideWaitDialog();

    public abstract ProgressDialog showWaitDialog();

    public abstract ProgressDialog showWaitDialog(int resid);

    public abstract ProgressDialog showWaitDialog(String text);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一:java概述:1,JDK:Java Development Kit,java的开发和运行环境,java的开发工...
    ZaneInTheSun阅读 7,645评论 0 11
  • 下标脚本 下标脚本 可以定义在类、结构体和枚举这些目标中,可以认为是访问集合(collection),列表(li...
    cht005288阅读 3,382评论 0 0
  • importUIKit classViewController:UITabBarController{ enumD...
    明哥_Young阅读 9,494评论 1 10
  • (一)Java部分 1、列举出JAVA中6个比较常用的包【天威诚信面试题】 【参考答案】 java.lang;ja...
    独云阅读 11,955评论 0 62
  • 接口/抽象类意义规范、扩展、回调为其子类提供一个公共的类型 封装子类中得重复内容 定义抽象方法,子类虽然有不同的实...
    MigrationUK阅读 6,745评论 1 28

友情链接更多精彩内容