Android知识android 开发程序员Android开发经验谈

Android app被回收之后会导致的问题

2017-12-06  本文已影响0人  超威蓝猫l

当android app被回收之后,再次点击图标会回到最后打开的页面,但是一些数据已经被回收了,直接打开可能会报空指针异常之类的,我们可以判断是否被回收,然后重启app

public class StatusHolder{
  public class StatusHolder {
    private static StatusHolder mInstance;
    private boolean isKill = true;

    public boolean isKill() {
        return isKill;
    }

    public void setKill(boolean kill) {
        isKill = kill;
    }

    private StatusHolder() {

    }

    public static StatusHolder getInstance() {
        if (mInstance == null) {
            synchronized (StatusHolder.class) {
                if (mInstance == null) {
                    mInstance = new StatusHolder();
                }
            }
        }
        return mInstance;
    }
}
}

再BaseActivity的oncreate里面判断是否是loadingActivity,如果不是的话就判断是不是已经被回收,被回收就重启app

if (mActivity instanceof LoadingActivity) {
            StatusHolder.getInstance().setKill(false);
        }else {
            if(StatusHolder.getInstance().isKill()) {
                LogUtils.e("app was kill");
                Intent intent = new Intent(this, LoadingActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
            }else {
                LogUtils.d("app was normal");
            }
        }

首先判断是不是初始化的页面 是的话就设置是否被杀为false,如果不是初始化页面就判断是否被杀,被杀就重启

上一篇下一篇

猜你喜欢

热点阅读