简体   繁体   English

屏幕改变方向时会调用什么? 更改方向后,我的应用程序正在重新启动

[英]What gets called when the screen changes orientation? My app is restarting when I change orientations

I have built an app that is a simple question and answer game. 我建立了一个简单的问答游戏应用程序。 A Question is displayed and you can select true or false, and then a toast appears to tell you if you got the question correct. 此时会显示一个问题,您可以选择是或否,然后出现祝酒词,告诉您问题是否正确。 You can then press a button to move to the next question. 然后,您可以按一个按钮移至下一个问题。 The problem is that when you rotate the screen (change the screens orientation) the app goes back to the first question displayed. 问题是,当您旋转屏幕(更改屏幕方向)时,应用程序会返回到显示的第一个问题。 What gets called to make this happen? 什么叫实现这一目标? I have three classes. 我有三节课。 The first is the MainActivity which launches the quiz layout. 第一个是MainActivity,它将启动测验布局。 The next is a Question class. 接下来是Question类。 The last is the QuizActivity Class. 最后一个是QuizActivity类。 Here is the code: 这是代码:

MainActivity: 主要活动:

package com.example.geoquiz;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
    }
}

Question Class: 问题类别:

package com.example.geoquiz;

public class Question {
    private int mTextResId;
    private boolean mAnswerTrue;
    public Question(int textResId, boolean answerTrue) {
        mTextResId = textResId;
        mAnswerTrue = answerTrue;
    }
    public int getTextResId() {
        return mTextResId;
    }
    public void setTextResId(int textResId) {
        mTextResId = textResId;
    }
    public boolean isAnswerTrue() {
        return mAnswerTrue;
    }
    public void setAnswerTrue(boolean answerTrue) {
        mAnswerTrue = answerTrue;
    }
}

QuizActivity Class: QuizActivity类别:

package com.example.geoquiz;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends AppCompatActivity {
    private Button mTrueButton;
    private Button mFalseButton;
    private Button mNextButton;
    private TextView mQuestionTextView;
    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);
    setContentView(R.layout.activity_quiz);
    mQuestionTextView = (TextView)    findViewById(R.id.question_text_view);
    mTrueButton = (Button) findViewById(R.id.true_button);
    mTrueButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) { checkAnswer(true); }
    });
    mFalseButton = (Button) findViewById(R.id.false_button);
    mFalseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) { checkAnswer(false); }
    });
    mNextButton = (Button) findViewById(R.id.next_button);
    mNextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
            updateQuestion();
        }
    });
    updateQuestion();
}
    private void updateQuestion() {
        int question = mQuestionBank[mCurrentIndex].getTextResId();
        mQuestionTextView.setText(question);
    }
    private void checkAnswer(boolean userPressedTrue) {
        boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
        int messageResId = 0;
        if (userPressedTrue == answerIsTrue) {
            messageResId = R.string.correct_toast;
        } else {
            messageResId = R.string.incorrect_toast;
        }
        Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
    }

} // to close the entire class

I believe the code to update the question begins in the QuizActivity class. 我相信更新问题的代码始于QuizActivity类。 I think the issue is that the counter for which question to be displayed is being set back to 0, but I do not know where to set a breakpoint to check this. 我认为问题在于将要显示问题的计数器设置为0,但是我不知道在哪里设置断点来检查该问题。 Whenever I set a breakpoint none of the UI is updated and I can therefore not test this. 每当我设置断点时,都不会更新任何UI,因此无法对此进行测试。

I also have the XML file if you want to test this for yourself: 如果您想自己测试一下,我也有XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:orientation="vertical">
<TextView
android:id="@+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/true_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/true_button" />

        <Button
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/false_button" />

    </LinearLayout>
    <Button
    android:id="@+id/next_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/next_button" />
</LinearLayout>

The problem is that when you rotate the screen (change the screens orientation) the app goes back to the first question displayed. 问题是,当您旋转屏幕(更改屏幕方向)时,应用程序会返回到显示的第一个问题。 What gets called to make this happen? 什么叫实现这一目标?

Your activity is destroyed and recreated as part of a configuration change . 作为配置更改的一部分,您的活动被销毁并重新创建。 Consider using a ViewModel to hold your activity's state and be able to retain it across configuration changes. 考虑使用ViewModel来保存活动的状态,并能够在配置更改中保留它。

您可以使用ViewModel或重写onSaveInstanceState(Bundle savedInstanceState)并将要更改的应用程序状态值写入到Bundle参数中,例如当前的问题编号,它将被传入onCreate()和onRestoreInstanceState()中,然后从中提取价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM