在android中的onPause()
方法中,确保Activity
恢复到以前的状态的正确方法是什么?
例如,如果我想暂停并恢复此Activity
:
/**
* Class containing an activity where the user enters an answer for each sum
* within the times tables
*
* @author Ross
*
*/
public class PracticeTest extends Activity implements View.OnClickListener {
// declare variables
int multiplier;
int[] results = new int[12];
int numberPassed;
TextView question;
EditText answer;
ImageView image;
int score;
String[] questions = new String[12];
MediaPlayer correctNoise;
MediaPlayer incorrectNoise;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.practicetest);
// / Setting int equal to int passed from previous activity (defaulted
// to zero)
numberPassed = getIntent().getIntExtra("convertedNumber2", 0);
// Initialising variables
correctNoise = MediaPlayer
.create(PracticeTest.this, R.raw.correctnoise);
incorrectNoise = MediaPlayer.create(PracticeTest.this,
R.raw.incorrectnoise);
Button submit = (Button) findViewById(R.id.btnGoPractice2);
answer = (EditText) findViewById(R.id.etEnterNumberPractice2);
question = (TextView) findViewById(R.id.tvTopPractice2);
image = (ImageView) findViewById(R.id.imageViewPracticeTest);
// Setting on click listener for submit button
submit.setOnClickListener(this);
// update the question
updateQuestion();
}
public void onClick(View view) {
// sets text view equal to what is entered in editText
final String entry = answer.getText().toString();
// convert from string value to int
int a = Integer.parseInt(entry); // note: maybe change name
results[multiplier - 1] = a;
// Displaying image and noise dependent on user answer
if (a == numberPassed * multiplier) {
correctNoise.start();
image.setImageResource(R.drawable.thumbsup);
score++;
} else {
incorrectNoise.start();
image.setImageResource(R.drawable.thumbsdown);
}
if (multiplier < 12) {
// called after an answer is given
updateQuestion();
} else {
Intent intent = new Intent(this, PracticeResults.class);
// Pass variables to next activity
intent.putExtra("results", results);
intent.putExtra("numberPassed", numberPassed);
intent.putExtra("questions", questions);
intent.putExtra("score", score);
// Start new activity via intent
this.startActivity(intent);
}
}
/**
* Method that updates question
*/
public void updateQuestion() {
// increase multiplier (i.e. 7x1 to 7x2 etc)
multiplier++;
// string to hold question
String q = numberPassed + "x" + multiplier + "=";
// Setting question equal to correct place in questions array
questions[multiplier - 1] = q;
// Set text in textView equal to the question
question.setText(questions[multiplier - 1]);
// change editText to be blank so a new answer can be entered
answer.setText("");
}
}
答案 0 :(得分:2)
请参阅此处http://developer.android.com/training/basics/activity-lifecycle/recreating.html
保存您的活动
static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...
@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
}
...
}
答案 1 :(得分:0)
您可以执行以下操作:
1)定义您的活动状态。状态是完整描述您的活动在任何给定时刻的外观和行为的所有数据。
在您的情况下,您需要保存question
和answer
以及multiplier
以及其他您从其他数据无法计算的内容。
2)覆盖onSaveInstanceState()
并将您的状态保存在捆绑参数
3)在onCreate
中,如果savedState
参数不为null,则从包中恢复状态,并根据恢复状态填充Activity的视图以及应该初始化的其他任何内容
通过这种方式,您可以保证如果您的活动在暂停时被销毁,数据永远不会丢失。
除此之外,暂停onPause
中的所有正在运行的流程,然后继续onResume