Search code examples
androidandroid-activityandroid-fragmentsfragmentscreen-rotation

onCreateView called two times on Fragment when I rotate the screen


I have an Activity with a NavigationDrawer and a Fragment which is created when I click on the NavigationDrawer. When I create the Fragment, I pass to it some arguments. When I rotate the screen, the activity is recreated, the navigationdrawer is recreated and also the Fragment. After this operations, the fragment is recreated, but without arguments. I want understand who creates the Fragment the second time... Can anyone help me?


Solution

  • Use this in your manifest with Fragment activity:

          android:configChanges="orientation|keyboardHidden|screenSize"
    

    However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device)