【问题标题】:Saving Data Across Rotation by override onSaveInstanceState通过覆盖 onSaveInstanceState 跨轮换保存数据
【发布时间】:2018-02-04 16:22:51
【问题描述】:

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

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);
    }
}

【问题讨论】:

  • 你试过onRestoreInstanceState了吗?
  • @KostasDrak 同样的事情。它显示不为空,但问题正在改变
  • 能不能放断点,检查索引值?
  • 如果显示不为空,可能问题不在于保存和恢复状态,而是您的索引或逻辑

标签: android onsaveinstancestate


【解决方案1】:

在我看来,你可能遗漏了什么。

由于 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();
        }
    });
}

【讨论】:

  • 我可以知道你有什么改变吗?我仍然得到相同的结果
  • 在你的代码中你调用超级然后保存,而我的第一个是保存,之后是超级调用
  • 但结果还是一样
  • 您可以添加一些打印并使用 mCurrentIndex 的值发布 logcat 吗?可能你的逻辑是错误的
  • 问题是你阅读时没有调用 updateQuestion
【解决方案2】:

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

<activity
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:name=".yourActivity"
    android:label="@string/app_name">
</activity>

【讨论】:

  • 这可以防止在此标志存在时重新创建 Activity。这没有回答问题
猜你喜欢
  • 2021-03-13
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
  • 1970-01-01
  • 1970-01-01
  • 2012-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多