2014-01-08 34 views
1

我正在嘗試創建一個基本應用程序,該程序只計算用戶將方向從縱向更改爲縱向的次數,並在屏幕上顯示計數。我有:Android計數用戶更改方向的次數

public class MainActivity extends Activity { 
int count = 0; 
private static boolean inLandscape = true; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    TextView tv = (TextView) findViewById(R.id.count); 
    tv.setText(getResources().getString(R.string.count) + ' ' + count); 
    if (!inLandscape && getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) 
    { 
     inLandscape = true; 
     count++; 
     Log.e("Debug","In Landscape " + count); 
    } 
    else if (inLandscape && getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) 
     inLandscape = false; 
} 

問題是,在我的調試語句中count總是1,並且文本在屏幕上永遠不會改變。做一些研究,我認爲這是因爲每當方向發生變化時,活動都會被丟棄並重新創建。如何在整個方位變化中保持變量值?

我嘗試使用savedInstanceState與

if (savedInstanceState.containsKey("count")) 
      count = savedInstanceState.getInt("count"); 
savedInstanceState.putInt("count", count); 

但是這給在containsKey線一個NullPointerException。

+0

請參閱http://stackoverflow.com/questions/8248274/android-detect-orientation-changed檢測東方變化 –

+0

我不想使用方向更改事件爲這個項目,因爲我使用佈局 - 土地和佈局端口文件。我寧願讓Android處理方向更改。 – awestover89

回答

1

覆蓋onSaveInstanceState存儲您的方向更改計數器。

@Override 
public void onSaveInstanceState(Bundle savedInstanceState) { 
    super.onSaveInstanceState(savedInstanceState); 
    savedInstanceState.putInt("count", count) 
} 

並且覆蓋onRestoreInstanceState在更改後將其讀回。

@Override 
public void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    count = savedInstanceState.getInt("count"); 
} 

很顯然,你仍然可以保持你的代碼在同一個地方的「的onCreate」,爲有條件地將每一個的onCreate被稱爲時間取決於方向的計數器。

1

它給你一個NullPointerException,因爲saveInstanceState爲null。您還需要將savedInstanceState.putInt("count", count)放入您的onRestoreInstanceState方法中。所以,你的代碼看起來像......

int mCount;  

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    TextView tv = (TextView) findViewById(R.id.count); 
    mCount = 0; 
    tv.setText(getResources().getString(R.string.count) + ' ' + mCount); 
} 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    mCount++; 
    tv.setText(getResources().getString(R.string.count) + ' ' + count); 
} 

public void onRestoreInstanceState(Bundle savedInstanceState) { 
    // Always call the superclass so it can restore the view hierarchy 
    super.onRestoreInstanceState(savedInstanceState); 
    mCount = savedInstanceState.getInt("count"); 
} 

@Override 
public void onSaveInstanceState(Bundle savedInstanceState) { 
    savedInstanceState.putInt("count", mCount); 

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

請參閱http://developer.android.com/training/basics/activity-lifecycle/recreating.htmlhttp://developer.android.com/guide/topics/resources/runtime-changes.html以獲取更多信息。