簡體   English   中英

從后台恢復時重新啟動應用程序

[英]Relaunch app when resuming from background

我可以阻止應用程序在被其他進程殺死時恢復嗎?

我有問題,我在Application classSingleton類實例,它通過app保存我的所有關於用戶和從API收集的東西的信息。 當我從后台打開應用程序時(如果它被某些進程殺死)我在用戶模型(圖片路徑,名稱...)和應用程序粉碎時變為null

對我來說最好的方法是從啟動器屏幕重新啟動應用程序並再次獲取所有信息,而不是恢復。 對此有何解決方案?

我可以阻止應用程序在被其他進程殺死時恢復嗎?

通過調用onCreateonStartonResume ,可以通過三種方式“恢復”應用程序。 調用哪一個取決於您的應用程序被“暫停”的方式,以及在此狀態下發生的情況。

onResume跟隨onPause ,當另一個應用程序被帶到前台時發生。

onStartonRestart )跟隨onStop ,當您的應用程序不再可見時會發生。

onCreate跟隨onPauseonStop如果你的內存丟失到另一個應用程序。

當我從后台打開應用程序時(如果它被某些進程殺死)我在用戶模型上獲得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);
}

並在onCreateonRestoreInstanceState恢復它

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM