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.

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  
}

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 !!!");
       }
   }

Need Your Help

How can I protect MySQL username and password from decompiling?

java mysql security reverse-engineering decompiling

Java .class files can be decompiled fairly easily. How can I protect my database if I have to use the login data in the code?

bash script, create array of all files in a directory

arrays bash shell while-loop

I have a directory myDir of many .html files. I am trying to create an array of all the files in the directory so I might be able to index the array and be able to refer to particular html files ...