Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to check for orientation change in an android fragment

In my app, I have a FragmentActivity with multiple Fragments all in portrait mode except for one specific Fragment. I move between Fragments through footer Views in the FragmentActivity.

I have a different layout (actually have the same name but different widget Views) for this specific Fragment when there is an orientation change. When in landscape mode, I want to remove specific widgets from the Fragment layout View and the footer View of the FragmentActivity and when back in portrait, add everything back.

I have saved all the data I need in onSavedInstanceState() in the Fragment, but where best should I possibly test for an orientation change so that I can restore the Views appropriately?

Not sure I can override onConfigurationChange() and test there in my Fragment because I don't have the android:configChanges="orientation" in my android Manifest.

like image 308
irobotxx Avatar asked Dec 14 '12 10:12

irobotxx


People also ask

How do I change the orientation of a fragment?

Use the following code line in the fragment where you want a specific (in this case portrait) orientation. getActivity(). setRequestedOrientation( ActivityInfo. SCREEN_ORIENTATION_PORTRAIT);

How can we detect the orientation of the screen in Android?

int orientation = display. getOrientation(); Check orientation as your way and use this to change orientation: setRequestedOrientation (ActivityInfo.

How do I check my orientation?

If the activity locks the display ( android:screenOrientation="portrait" ), this method will return the same value irrespective of how the user rotates the device. In that case you'd use the accelerometer or the gravity sensor to figure out orientation properly.

What happens when screen orientation changes in Android?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.


2 Answers

Keep this check in onCreate(). When you rotate your device the app is restarted. So onCreate is called again when the app restarts.

int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
   // Landscape
}
else {
   // Portrait  
}
like image 131
Vikram Gupta Avatar answered Oct 13 '22 16:10

Vikram Gupta


You can control the orientation change by overriding onConfigurationChanged function of your fragment as well.

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
        int currentOrientation = getResources().getConfiguration().orientation;

        if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE){
           Log.v("TAG","Landscape !!!");
        }
        else { 
           Log.v("TAG","Portrait !!!");
       }
   }
like image 45
ACengiz Avatar answered Oct 13 '22 15:10

ACengiz