Android:APP长时间后台,再打开崩溃的问题

Android:APP长时间后台,再打开崩溃的问题

导致原因

后台时间过长,activity被回收或其它数据被回收。
当调至前台时,或重新打开时,需要使用之前的数据,所以,就报异常了。

解决办法

网上大多解决这种问题的做法是使用onSaveInstanceState和onRestoreInstanceState来保存UI状态的,基本上就是在按home键或者其他情况的时候存储数据,然后再次点开APP的时候读取bundle的数据。不过一般一个项目都有很多页面,存储数据这种方法就会很麻烦。

这里介绍个简单粗暴的方法:activity加载布局之前判断当前程序是否被系统回收,如果是则重新启动app。

首先新建AppStatus 类

public class AppStatus {
    public static final int STATUS_RECYVLE =-1; //被回收
    public static final int STATUS_NORMAL=1;    //正常
}

新建AppStatusManager类控制状态

public class AppStatusManager {
    public int appStatus = AppStatus.STATUS_RECYCLE;    //APP状态 初始值为不在前台状态

    private static AppStatusManager appStatusManager;

    private AppStatusManager(){}

	//单例模式
    public static AppStatusManager getInstance() {
        if (appStatusManager == null) {
            appStatusManager = new AppStatusManager();
        }
        return appStatusManager;
    }

    public int getAppStatus() {
        return appStatus;
    }

    public void setAppStatus(int appStatus) {
        this.appStatus = appStatus;
    }
}

闪屏页修改状态

    public class SplashActivity extends AppCompatActivity {

   	private ImageView sp;
   	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.startup);
        
        sp=(ImageView) findViewById(R.id.imageView);
        //AlphaAnimation anima = new AlphaAnimation(0.3f, 1.0f);
        //anima.setDuration(2500);// 设置动画显示时间
        //sp.startAnimation(anima);
        //anima.setAnimationListener( new MyAnimationListener());
        
        sp.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            	//app状态改为正常
            	AppStatusManager.getInstance().setAppStatus(AppStatus.STATUS_NORMAL);
            	startActivity(intent);
            	finish();
            }
        }, 2500);
    }
}

清单文件中MainActivity的启动模式要设为singleTask

<activity 
		android:name=".MainActivity"
		android:launchMode="singleTask" />

MainActivity:

public class MainActivity extends AppCompatActivity {
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (AppStatusManager.getInstance().getAppStatus() == AppStatus.STATUS_RECYCLE){
            //跳到闪屏页
            Intent intent = new Intent(this, SplashActivity.class);
            startActivity(intent);
            finish();
            return;
        }
        setContentView(R.layout.activity_main);
        Button bt = (Button) findViewById(R.id.bt);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, TestActivity.class);
            	startActivity(intent);
            }
        });
    }
}

新建一个BaseActivity继承AppCompatActivity,其他Activity继承BaseActivity

public class BaseActivity extends AppCompatActivity {
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (AppStatusManager.getInstance().getAppStatus() == AppStatus.STATUS_RECYCLE){
            //跳到MainActivity,让MainActivity也finish掉
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
            return;
        }
    }
}
public class TestActivity extends BaseActivity {
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
    }
}

你可能感兴趣的:(Android,android,后台运行,空指针,异常)