androidscreen-rotation

How to handle screen rotation with bottom navigation menu where each menu refers to a new viewpager with limited tabs (3-4)? I am Using ViewModel


I am having Bottom navigation menu, and each menu is having their separate viewpager with 3 tabs. Everything works fine in portrait mode but once rotate the device Viewpager is reset to that of first menu item.

To be specific I am on second bottom menu item and in second page of viewpager and i want to restore this state. Also keeping in mind the re usability of fragment. I am also using VIewModel, so it would be great if solution may take help of Viewmodel if possible.


Solution

  • You can add configChanges config to your activity specified in the android manifest file.

    <activity name= ".YourActivity" android:configChanges="orientation|screenSize"/>
    

    It'll handle the state for you automatically.

    Edit: Read more about Restrict activity recreation at https://developer.android.com/guide/topics/resources/runtime-changes#restrict-activity