Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Fragment onCreateView method call again after orientation changed

I implement the app which has two layout for landscape and portrait mode. The layout for landscape is in the layout-land. I have the fragment1 for portrait layout and fragment2 for landscape layouts. I override the onCreateView in each fragment.

I have static variable to know the current fragment. I assgined in the onCreateView (1 for fragment1 and 2 for fragment2).

My problem is that the static value is still 1 when the orientation is landscape mode.

I debugged the orientation of application. When I change orientation portrait into landscape, fragment2's onCreateView method called first and then the fragment1's onCreateView method called again. The static value has overridden.

I don't know why did fragment1 onCreateView method call after the fragment2 called? I want to assign the right value for right fragment.

Sorry for my bad English.

Thanks.

like image 286
user1156041 Avatar asked Feb 02 '26 18:02

user1156041


1 Answers

You don`t need to save something! Just let your activity handle the orientation change. In AndroidManifest.xml put this

    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|screenSize" >
    </activity>
like image 66
Edward Avatar answered Feb 05 '26 06:02

Edward



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!