簡體   English   中英

在物理設備中未調用onResume(),而是調用了onCreate()

[英]onResume() is not called in physical device instead onCreate() is called

我對此有點驚訝。我的活動中有一個onResume()。它在模擬器中被調用並運行良好,但是在物理設備中安裝了軟糖的三星銀河筆記中,它沒有被調用,而是被稱為onCreate()一直如此為什么會這樣?

public void onResume(){
    super.onResume();
    if(firsttime){
        try {
            Toast.makeText(getApplicationContext(), "Resuming Activity",Toast.LENGTH_LONG).show();
            addReminder();
        } catch(Exception exception) {
            exception.printStackTrace();
        }
    } else {
        firsttime=true;
    }
}

這是我的代碼。firsttime是一個靜態布爾變量,用於防止在首次啟動應用程序時調用onResume()

嘗試在onResume內打印某些內容,然后在LogCat中檢查它。...onResume內的代碼可能會導致這種情況。 否則您可以詳細說明您的問題嗎?

考慮到您當前的情況,由於生命周期取決於很多事情,因此您應該將變量保存在首選項中,而不要依賴活動生命周期。 通常,在這種情況下使用靜態變量是錯誤的選擇。我認為這應該可以解決您的問題。

我認為這是發生了什么,當您的應用程序不是頂級應用程序時,活動管理器實際上破壞了活動,它只調用了

    public void onSaveInstanceState(Bundle savedInstanceState)

沒有

    onStop

叫,所以沒有

    noResume

將被稱為。

正確的做法是,當將該活動的所有狀態置於

    public void onSaveInstanceState(Bundle savedInstanceState)

叫。

並在您的onCreate()函數中執行此操作

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}

檢查您是否有一些保存狀態。

大多數代碼是從android開發人員網站上復制的: http : //developer.android.com/training/basics/activity-lifecycle/recreating.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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