简体   繁体   中英

portrait and landscape orientations and set android:configChanges=“orientation” in fragment

I have specific layout & layout-land files for both portrait and landscape orientations and set android:configChanges="orientation" to the activity in manifest file to stop oncreate() to execute in fragment. Android will automatically destroy and recreate activity on an orientation change, and so each call to onCreate() will get the right layout. I have Main ActivityFragment which contains 4 Tab having fragments as orientation changes onCreate() get call and re-create it.But I dont want to get call to onCreate() but need the layout orientation change using layout & layout-land files.

Is any way to achieve this..

The following method is called when you override using configChanges.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Check the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        setContentView(R.layout.activity_layout);
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        setContentView(R.layout.activity_layout );
    }
}

As you know, if you set android:configChanges="orientation" Then on orientation change the activity and in turn fragments are not going to be restarted.

So, if you are in portrait mode and change to landscape mode, it will retain your portrait layout itself. so your landscape mode layout(from layout-land) will not be used. And similarly in case from landscape mode to portrait mode.

So, What you are trying to achieve is not the ideal approach.

Since, you want the layout to change, you must save the states(data) of each fragment and onConfigChanges populate the data back so that appropriate layout is selected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM