[英]Relaunch app when resuming from background
我可以阻止應用程序在被其他進程殺死時恢復嗎?
我有問題,我在Application class
有Singleton
類實例,它通過app保存我的所有關於用戶和從API
收集的東西的信息。 當我從后台打開應用程序時(如果它被某些進程殺死)我在用戶模型(圖片路徑,名稱...)和應用程序粉碎時變為null
。
對我來說最好的方法是從啟動器屏幕重新啟動應用程序並再次獲取所有信息,而不是恢復。 對此有何解決方案?
我可以阻止應用程序在被其他進程殺死時恢復嗎?
通過調用onCreate
, onStart
或onResume
,可以通過三種方式“恢復”應用程序。 調用哪一個取決於您的應用程序被“暫停”的方式,以及在此狀態下發生的情況。
onResume
跟隨onPause
,當另一個應用程序被帶到前台時發生。
onStart
( onRestart
)跟隨onStop
,當您的應用程序不再可見時會發生。
onCreate
跟隨onPause
或onStop
如果你的內存丟失到另一個應用程序。
當我從后台打開應用程序時(如果它被某些進程殺死)我在用戶模型上獲得Null(圖片路徑,名稱......)
嘗試在onSaveInstanceState
保留此用戶數據
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString(STATE_USER, mUser);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
並在onCreate
或onRestoreInstanceState
恢復它
static final String STATE_USER = "user";
private String mUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore user data from saved state
mUser = savedInstanceState.getString(STATE_USER);
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
text.setText(savedInstanceState.getString(STATE_USER));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.