2015-06-14 49 views
-1

我正在Twitter的整合的樣本,我取按鈕的點擊用戶信息如何使用onSavedInstance方法來防止數據丟失。數據正在屏幕上顯示,它與肖像模式工作正常,但我更改爲橫向模式,數據丟失。而改變爲橫向模式

如何解決這個問題,我不知道如何使用onSavedInstance方法來解決這個問題。請幫助

回答

1

你需要重寫像onSaveInstanceStateonRestoreInstanceState檢查這個例子的Android默認的方法。

@Override 
    public void onSaveInstanceState(Bundle savedInstanceState) { 
     // Save the user's current game state 
     savedInstanceState.putInt(STATE_SCORE, mCurrentScore); 
     savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel); 

     // Always call the superclass so it can save the view hierarchy state 
     super.onSaveInstanceState(savedInstanceState); 
    } 

    @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 
     } 
     ... 
    } 

    //you can retrieve saved values from here 
    public void onRestoreInstanceState(Bundle savedInstanceState) { 
     // Always call the superclass so it can restore the view hierarchy 
     super.onRestoreInstanceState(savedInstanceState); 

     // Restore state members from saved instance 
     mCurrentScore = savedInstanceState.getInt(STATE_SCORE); 
     mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); 
    } 

click here to find more about this