1

我创建了一个简单的应用程序。单击下一个按钮时,问题将被更改。如果屏幕旋转,我希望问题仍然保持不变。我已经通过下面的代码覆盖了 Activity 方法,protected void onSaveInstanceState(Bundle outState)但似乎没有任何效果。

public class QuizActivity extends AppCompatActivity {

    private Button mTrueButton, mFalseButton;
    private TextView mQuestionTextView;
    private static final String TAG = "QuizActivity";
    private static final String KEY_INDEX = "index";

    private Question[] mQuestionBank = new Question[]{
            new Question(R.string.question_australia, true),
            new Question(R.string.question_oceans, true),
            new Question(R.string.question_mideast, false),
            new Question(R.string.question_africa, false),
            new Question(R.string.question_americas, true),
            new Question(R.string.question_asia, true),
    };
    private int mCurrentIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate(Bundle) called");
        setContentView(R.layout.activity_main);
        if (savedInstanceState != null) {
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
            Toast.makeText(getApplication(),"not null",Toast.LENGTH_LONG).show();
        }else
        {
            Toast.makeText(getApplication(),"null",Toast.LENGTH_LONG).show();
        }
        mTrueButton = (Button) findViewById(R.id.true_button);
        mFalseButton = (Button) findViewById(R.id.false_button);
        mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
        ImageButton mNextButton = (ImageButton) findViewById(R.id.next_button);
        ImageButton mPreviousButton = (ImageButton) findViewById(R.id.previous_button);

        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
                updateQuestion();
            }
        });
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        Toast.makeText(getApplication(),"onSaveInstance",Toast.LENGTH_LONG).show();
        savedInstanceState.putInt(KEY_INDEX, mCurrentIndex);
    }

    public void updateQuestion() {
        int question = mQuestionBank[mCurrentIndex].getTextResId();
        mQuestionTextView.setText(question);
    }
}
4

2 回答 2

0

在我看来,也许你错过了一些东西。

由于 Activity 的生命周期是:

onPause()->onSaveInstanceState()->onStop()->onCreate()->onStart()->onRestoreInstanceState()->onResume()

你似乎做事正确,除了这个:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save current state
    savedInstanceState.putInt(KEY_INDEX, mCurrentIndex);
    Toast.makeText(getApplication(),"onSaveInstance",Toast.LENGTH_LONG).show();
    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

然后在你onCreate()做如下

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate(Bundle) called");
    setContentView(R.layout.activity_main);
    if (savedInstanceState != null) {
        mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
        Toast.makeText(getApplication(),"not null",Toast.LENGTH_LONG).show();
    }else
    {
        Toast.makeText(getApplication(),"null",Toast.LENGTH_LONG).show();
    }
    mTrueButton = (Button) findViewById(R.id.true_button);
    mFalseButton = (Button) findViewById(R.id.false_button);
    mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
    ImageButton mNextButton = (ImageButton) findViewById(R.id.next_button);
    ImageButton mPreviousButton = (ImageButton) findViewById(R.id.previous_button);

    mNextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
            updateQuestion();
        }
    });
}
于 2018-02-04T16:39:30.233 回答
-1

忘了onSaveInstanceState。只需在清单中使用此代码

<activity
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:name=".yourActivity"
    android:label="@string/app_name">
</activity>
于 2018-02-04T16:34:09.010 回答