Software⏱️ 2 min read📅 2026-05-30

How to Fix: IllegalStateException: Can not perform this action after onSaveInstanceState with ViewPager

The exception occurs when trying to perform an action after the activity has been paused and resumed due to onSaveInstanceState. This is often caused by using a ViewPager with Fragments that contain views that are not properly restored when the activity is recreated.

Quick Answer: Use the setRetainState() method on your FragmentManager to preserve the state of your fragments when the activity is paused, or use the setAllowStateLoss() method to allow the fragment manager to handle the loss of state.

The IllegalStateException: Can not perform this action after onSaveInstanceState with ViewPager error typically occurs when you're trying to save the state of your fragments while navigating away from them. This is because the ViewPager doesn't support saving its own state, and it relies on the Activity's lifecycle methods.

⚠️ Common Causes

  • Using a ViewPager in an Activity that has multiple fragments.

🛠️ Step-by-Step Verified Fixes

Method 1: Disable ViewPager's State Saving

  1. Step 1: In your Activity, set the android:id attribute of your ViewPager to null in the onSaveInstanceState method.

Method 2: Use a FragmentStatePagerAdapter

  1. Step 1: Replace your ViewPager's Adapter with a FragmentStatePagerAdapter.

💡 Conclusion

By following these steps, you can resolve the IllegalStateException: Can not perform this action after onSaveInstanceState with ViewPager error in your app.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions