通过重写onSaveInstanceState实现屏幕旋转数据留存问题求助
Hey there! Let's dig into why your onSaveInstanceState approach isn't keeping the current question when the screen rotates. I've run into this exact scenario before, so here are the most common fixes to get it working:
1. Make Sure You're Saving State Correctly (And Calling Super!)
First, double-check your onSaveInstanceState implementation. It's easy to forget calling the super method, which is critical for the system to handle default view state saving. Also, ensure you're storing the current question identifier (like an index or ID) in the Bundle:
@Override protected void onSaveInstanceState(Bundle outState) { // Don't skip this line! It lets the system save default view states super.onSaveInstanceState(outState); // Replace currentQuestionIndex with your actual variable tracking the active question outState.putInt("ACTIVE_QUESTION_INDEX", currentQuestionIndex); }
2. Restore the State When the Activity Re-Creates
Saving the state is only half the battle—you need to load it back when the Activity rebuilds after rotation. You have two reliable ways to do this:
Option A: Restore in onCreate
Check if the savedInstanceState Bundle isn't null, then pull your saved value and update the UI:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_activity_layout); if (savedInstanceState != null) { // Get the saved index, default to 0 if the key doesn't exist currentQuestionIndex = savedInstanceState.getInt("ACTIVE_QUESTION_INDEX", 0); // Refresh the UI to show the correct question displayCurrentQuestion(); } else { // First launch: start with the first question currentQuestionIndex = 0; displayCurrentQuestion(); } }
Option B: Use onRestoreInstanceState
This method is specifically designed for state restoration, and it's called after onStart()—so it's a clean, dedicated place to handle this:
@Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); currentQuestionIndex = savedInstanceState.getInt("ACTIVE_QUESTION_INDEX", 0); displayCurrentQuestion(); }
3. Check for configChanges in Your Manifest
If you added android:configChanges="orientation|screenSize" to your Activity in AndroidManifest.xml, the system won't rebuild your Activity when the screen rotates. That means onSaveInstanceState won't be triggered at all!
If this is the case, switch to handling rotation with onConfigurationChanged:
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Since the Activity isn't recreated, your currentQuestionIndex variable is still valid // Just refresh the UI to fit the new screen orientation displayCurrentQuestion(); }
Quick Sanity Check
Make sure currentQuestionIndex is a member variable of your Activity (not a local variable inside a method). Local variables get destroyed when the Activity is recreated, so they can't hold state across rotation.
内容的提问来源于stack exchange,提问作者John Joe

