Android activity object manages global status bar settings baseactivity settings

In our development process, in order to save the code for the convenience of use, you will definitely write a baseactivity class to provide other activities to use. First of all, we need to set the color of the status bar. The way is for your reference
ompile 'com.githang:status-bar-compat:latest.integration' Add dependencies
Create baseactivity and override the oncreate() method
//Set the status bar color
StatusBarCompat.setStatusBarColor(this, getResources().getColor(R .color.color_orange), true);
Then set the app style style NoActionBar, then the status bar can be the color you want
and then talk about the activity stack management

Create a management class appmanager

The code is given below

**

public class AppManager {
    public static Stack<Activity> activityStack;
    private static AppManager instance;
    private AppManager(){}
    /**
     * 单一实例
     */
    public static AppManager getAppManager(){
        if(instance==null){
            instance=new AppManager();
        }
        return instance;
    }
    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity){
        if(activityStack==null){
            activityStack=new Stack<Activity>();
        }
        activityStack.add(activity);
    }
    /**
     * 获取当前Activity(堆栈中最后一个压入的)
     */
    public Activity currentActivity(){
        Activity activity=activityStack.lastElement();
        return activity;
    }
    /**
     * 结束当前Activity(堆栈中最后一个压入的)
     */
    public void finishActivity(){
        Activity activity=activityStack.lastElement();
        if(activity!=null){
            activity.finish();
            activity=null;
        }
    }
    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity){
        if(activity!=null){
            activityStack.remove(activity);
            activity.finish();
            activity=null;
        }
    }
    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls){
        for (Activity activity : activityStack) {
            if(activity.getClass().equals(cls) ){
                finishActivity(activity);
            }
        }
    }
    /**
     * 结束所有Activity
     */
    public void finishAllActivity(){
        for (int i = 0, size = activityStack.size(); i < size; i++){
            if (null != activityStack.get(i)){
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }
    /**
     * 退出应用程序 时间间隔2m内点击物理返回键 直接推出app 2m 以外则重新清零计时 当前堆栈内只有一个activity对象时 就提示是否退出app
     */
    public void AppExit(Context context) {
        try {
            if (activityStack!=null){
                Toast.makeText(context, activityStack.size() + "", Toast.LENGTH_SHORT).show();
            }
            finishAllActivity();
            ActivityManager activityMgr= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.restartPackage(context.getPackageName());
            System.exit(0);


        } catch (Exception e) { }
    }
}

**
The comment has been very detailed, so I won't say more here

This is the management class that provides several methods to call according to requirements
and then write baseactivity

public class BaseActivity extends AppCompatActivity implements View.OnClickListener{
    public Context context;
    private static final List<String> options1Items = new ArrayList<>();
    public Toast toast;
    public SharedPreferences sharedPreferences;
    public String mobileId;
    public SPUtils spUtils;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        spUtils = new SPUtils(context);



        //设置状态栏颜色
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.color_orange), true);
        AppManager.getAppManager().addActivity(this);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        setContentView();
        initViewP();
        initData();
        initListenerP();

    }

    public View onFindView(int id) {
        View view = findViewById(id);
        return view;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
    long firstTime=0;
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){     //KEYCODE_BACK:回退键
            if (AppManager.activityStack!=null&&AppManager.activityStack.size()==1){// 判断堆栈集合 并且长度为1时 执行推出app
                long secondTime= System.currentTimeMillis();
                if (secondTime-firstTime>2000){
                    Toast.makeText(AppManager.getAppManager().currentActivity(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                    firstTime=System.currentTimeMillis();
                    return true;
                }else{
                    AppManager.getAppManager().AppExit(getApplicationContext());
                    spUtils.clearSpSpace();
                }
            }
        }
        return super.onKeyUp(keyCode, event);
    }




    @Override
    protected void onDestroy() {
        super.onDestroy();
        //将Activity实例从AppManager的堆栈中移除
        AppManager.getAppManager().finishActivity(this);

    }
        // toast 提示
    public void showToast(String text) {
        if (toast == null) {
            toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        } else {
            toast.setText(text);
        }
        toast.show();
    }

    public void hintWindows(String hint){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View inflate = LayoutInflater.from(context).inflate(R.layout.base_hint_layout, null);
        builder.setView(inflate);
        TextView hint_tv = (TextView) inflate.findViewById(R.id.basehint_content_tv);
        Button cancel_bt = (Button) inflate.findViewById(R.id.basehint_cancel_bt);
        Button sure_bt = (Button) inflate.findViewById(R.id.basehint_sure_bt);

        hint_tv.setText(hint);
        final AlertDialog dialog = builder.create();
        cancel_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();

            }
        });
        sure_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast("确定");
            }
        });
        dialog.show();
    }
    @Override
    public void onClick(View v) {
        //继承了BaseActivity的类, 如果要使用返回关闭Activity的功能
        //需要在继承的Activity的onClick(View v)里写上super.onClick(v);
        switch (v.getId()) {
            case R.id.bt_back:
                //R.id.back为标题左上角的返回控件
                onBackPressed();
                break;
            case R.id.bt_shopcar:
                startActivity(new Intent(context, ShopCarActivity.class));

                break;
            default:
                break;
        }
    }

    /*
    * 加载布局
    * */
    public  void setContentView(){};
    /*
    * 初始化视图
    * */
    public void initViewP(){};
    /*
    * 初始化监听
    * */
    public void initListenerP(){};
    /*
    * 初始化数据
    * */
    public void initData(){};

}

This is my baseactivity. I can add trying to initialize the view, initialize the data, add monitoring, etc. I also create a global sp object, manage configuration data, global toast, global dialog, etc. You can also configure network request progress control methods, etc. See your needs In short, it is for convenience and conciseness. Tool packaging
about sp—""" https://blog.csdn.net/naide_s/article/details/79974617

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325684548&siteId=291194637