I have a question about rotating the Android device. My code logs a static and non-static attribute in onCreate(...).
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
static int sn;
int n;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sn++;
n++;
Log.i("onCreate", String.format("sn=%d n=%d", sn, n));
}
}
The screen orientation is portrait. When I first ran the code, I got:
onCreate(): sn=1 n=1
After I rotated the screen to landscape, I got:
onCreate(): sn=2 n=1
After I rotated the screen again to portrait, I got:
onCreate(): sn=3 n=1
onCreate(): sn=4 n=1
My questions are:
- How can I prevent onCreate(...) to be called twice when the device is rotated back to portrait?
- How can I save the value of non-static variable when the device is rotated?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…