Turn off auto rotation in fragment

In your Fragment call inside onResume to lock to portrait:

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

then in onPause to unlock orientation:

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);

OBS! For the sake, use if(getActivity != null) before using this methods.


Simple solution using Googles Navigation Conponents:

In your Activity:

navController.addOnDestinationChangedListener { _, destination, _ ->
        if (destination.id == R.id.fragmentB) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        } else if (requestedOrientation != ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
        }
    }

This logic would lock the screen orientation of "fragmentB" (Id resource from your navigation graph) to portrait.

navController is an instance of androidx.navigation.NavController