21

I have a PictureFragment which I use to show a picture in fullscreen when selecting it from my thumbnail. It works fine, but when I rotate my smartphone, the picture also rotates and gets scaled very ugly sothat its height is now its actual width and so on. How can I turn off the rotation for this fragment? I've read always how to do it for a whole activity but for the rest of the activity this runs in I want to keep the auto rotation. Or, if this is also easy possible, how can I manage to scale the picture sensefully on rotation to keep its aspect ratio?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Better to set `Orientation` to your `Activity`. – M D Apr 14 '15 at 09:27
  • I have only one activtiy and switch between screens via fragmentmanager. And why do I get thumbs down for this question? –  Apr 14 '15 at 09:30
  • I think this may answer your question: http://stackoverflow.com/questions/13305861/fool-proof-way-to-handle-fragment-on-orientation-change – Abdul Rahman A Samad Apr 14 '15 at 09:36
  • thank you. another question: is it senceful to use very less activities (one or two) and do a lot of screenchanging stuff with the fragment manager? Or is it better to use an activity for every screen? –  Apr 14 '15 at 09:43
  • @MD it's a bit old, but what if you only use one activity? or is that bad in general. – JochemQuery Jun 07 '15 at 11:29

3 Answers3

40

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.

Carnal
  • 21,744
  • 6
  • 60
  • 75
  • 1
    I found myself in an endless activity restart loop if I navigate from FragmentA (IN landscape mode) to FragmentB (containing portait lock logic from top) – Palm Jan 04 '21 at 08:57
3

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

Palm
  • 699
  • 1
  • 6
  • 17
0

Add in manifest

android:configChanges="keyboardHidden|orientation"

add in your fragment

@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (getActivity() != null) {          
            if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
                getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
        }       
}