考虑下面的mCreate等,int
变量是动态增加的mCreate++
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt(CREATE_KEY, mCreate);
savedInstanceState.putInt( RESTART_KEY, mRestart);
savedInstanceState.putInt(START_KEY, mStart);
savedInstanceState.putInt(RESUME_KEY, mResume);
super.onSaveInstanceState(savedInstanceState);
}
//////////// get
if (savedInstanceState != null) {
mCreate = savedInstanceState.getInt(CREATE_KEY, mCreate);
mRestart = savedInstanceState.getInt( RESTART_KEY, mRestart);
mStart = savedInstanceState.getInt(START_KEY, mStart);
mResume = savedInstanceState.getInt(RESUME_KEY, mResume);
}
。我需要存储和检索他们最新的递增值。我认为我做得对,但有些人如何不应用最新的递增值。
{{1}}
答案 0 :(得分:4)
代码应该是这样的:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt(CREATE_KEY, mCreate);
savedInstanceState.putInt(RESTART_KEY, mRestart);
savedInstanceState.putInt(START_KEY, mStart);
savedInstanceState.putInt(RESUME_KEY, mResume);
}
if (savedInstanceState != null) {
mCreate = savedInstanceState.getInt(CREATE_KEY);
mRestart = savedInstanceState.getInt(RESTART_KEY);
mStart = savedInstanceState.getInt(START_KEY);
mResume = savedInstanceState.getInt(RESUME_KEY);
}
答案 1 :(得分:1)
你想要这个。
private TextView mTvCreate;
private TextView mTvStart;
private TextView mTvResume;
private TextView mTvRestart;
private int mCreate = 0;
private int mStart = 0;
private int mResume = 0;
private int mRestart = 0;
// TODO: Create variables for each of the TextViews, called
// mTvCreate, etc.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
// TODO: Assign the appropriate TextViews to the TextView variables
// Hint: Access the TextView by calling Activity's findViewById()
// textView1 = (TextView) findViewById(R.id.textView1);
mTvCreate = (TextView) findViewById(R.id.create);
mTvStart = (TextView) findViewById(R.id.start);
mTvResume = (TextView) findViewById(R.id.resume);
mTvRestart = (TextView) findViewById(R.id.restart);
Button launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo);
launchActivityTwoButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO:
// Launch Activity Two
// Hint: use Context's startActivity() method
// Create an intent stating which Activity you would like to start
// Launch the Activity using the intent
Intent startActivity = new Intent(ActivityOne.this, ActivityTwo.class);
startActivity(startActivity);
}
});
// Check for previously saved state
if (savedInstanceState != null) {
// TODO:
// Restore value of counters from saved state
// Only need 4 lines of code, one for every count variable
mCreate = savedInstanceState.getInt(CREATE_KEY);
mStart = savedInstanceState.getInt(START_KEY);
mResume = savedInstanceState.getInt(RESUME_KEY);
mRestart = savedInstanceState.getInt(RESTART_KEY);
}
++mCreate;
// TODO: Emit LogCat message
Log.i(TAG, "onCreate() "+mCreate);
displayCounts();
// TODO:
// Update the appropriate count variable
// Update the user interface via the displayCounts() method
}
// Lifecycle callback overrides
@Override
public void onStart() {
super.onStart();
// TODO: Emit LogCat message
++mStart;
Log.i(TAG, "onStart() "+mStart);
// TODO:
// Update the appropriate count variable
// Update the user interface
displayCounts();
}
@Override
public void onResume() {
super.onResume();
// TODO: Emit LogCat message
++mResume;
Log.i(TAG, "onResume() "+mResume);
// TODO:
// Update the appropriate count variable
// Update the user interface
displayCounts();
}
@Override
public void onPause() {
super.onPause();
// TODO: Emit LogCat message
}
@Override
public void onStop() {
super.onStop();
// TODO: Emit LogCat message
}
@Override
public void onRestart() {
super.onRestart();
// TODO: Emit LogCat message
++mRestart;
// TODO:
// Update the appropriate count variable
// Update the user interface
Log.i(TAG, "onRestart() "+mRestart);
displayCounts();
}
@Override
public void onDestroy() {
super.onDestroy();
// TODO: Emit LogCat message
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// TODO:
// Save state information with a collection of key-value pairs
// 4 lines of code, one for every count variable
savedInstanceState.putInt(CREATE_KEY, mCreate);
savedInstanceState.putInt(START_KEY, mStart);
savedInstanceState.putInt(RESUME_KEY, mResume);
savedInstanceState.putInt(RESTART_KEY, mRestart);
}
// Updates the displayed counters
public void displayCounts() {
mTvCreate.setText("onCreate() calls: " + mCreate);
mTvStart.setText("onStart() calls: " + mStart);
mTvResume.setText("onResume() calls: " + mResume);
mTvRestart.setText("onRestart() calls: " + mRestart);
}