Skip to content

Instantly share code, notes, and snippets.

@soolaugust
Last active Jan 10, 2022
Embed
What would you like to do?
Android fix app crash when kill by system in background; Android项目长时间在后台运行后,再次打开程序崩溃问题完美解决(APP在后台被系统回收后,如何重新启动)
public class AppStatus {
public static final int STATUS_RECYCLE =-1; //被回收
public static final int STATUS_NORMAL=1; //正常
}
public class AppStatusManager {
public int appStatus = AppStatus.STATUS_RECYCLE;//APP状态 初始值被系统回收
public 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 BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//判断app状态
if (AppStatusManager.getInstance().getAppStatus() == AppStatus.STATUS_RECYCLE){
//被回收,跳转到启动页面
Intent intent = new Intent(this, StartActivity.class);
startActivity(intent);
finish();
return;
}
}
}
public class StartActivity extends AppCompatActivity {
private ImageView sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startup);
sp=(ImageView) findViewById(R.id.imageView);
sp.postDelayed(new Runnable() {
@Override
public void run() {
//页面跳转
Intent intent = new Intent(StartActivity.this, MainActivity.class);
//app状态改为正常
AppStatusManager.getInstance().setAppStatus(AppStatus.STATUS_NORMAL);
startActivity(intent);
finish();
}
}, 2500);
}
}
public class TestActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
}
@HWilliamgo
Copy link

HWilliamgo commented Jan 5, 2022

StartActivity第17行,这里应该不是SplashActivity.this,而是StartActivity.this

@soolaugust
Copy link
Author

soolaugust commented Jan 5, 2022

@HWilliamgo 谢谢, 已更新

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment