Last active
June 6, 2024 13:29
-
-
Save soolaugust/f8355f9070af76de071168ad7a410bbe to your computer and use it in GitHub Desktop.
Android fix app crash when kill by system in background; Android项目长时间在后台运行后,再次打开程序崩溃问题完美解决(APP在后台被系统回收后,如何重新启动)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AppStatus { | |
public static final int STATUS_RECYCLE =-1; //被回收 | |
public static final int STATUS_NORMAL=1; //正常 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestActivity extends BaseActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_test); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
StartActivity第17行,这里应该不是SplashActivity.this,而是StartActivity.this