5

I have an issue very similar to this: Android - ActionBar not resizing with onConfigurationChanged ( AppCompat )

I need to let android handle orientation changes, because i want to get the activity recreated.

But also, i need to detect when a orientation change has been produced.

Are these two combined needs able to be achieved at once?

Community
  • 1
  • 1
NullPointerException
  • 34,337
  • 71
  • 211
  • 357

1 Answers1

4

To detect the rotation of the Activity without interfering with its course, you can do the following :

@Override
public void onConfigurationChanged(Configuration config) {
    super.onConfigurationChanged(config);
    // Check for the rotation
    if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "LANDSCAPE", Toast.LENGTH_SHORT).show();
    } else if (config.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "PORTRAIT", Toast.LENGTH_SHORT).show();
    }
}

Additionally, you need you have to make sure that, on your manifest, your activity allows configChanges for the rotation

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name">

Important : As of Android 3.2 onwards (API 13+) , the "screen size" also changes when the device switches between portrait and landscape orientation. If you want to prevent runtime restarts due to orientation change when developing for that specific API level (13+), make sure to declare : android:configChanges="orientation|screenSize"

-- // --

EDIT

Using the configChanges attribute, your Activity will not be recreated when configuration changes.

More can be found on Google's Documentation here

android:configChanges

Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.

What you can do is control the rotation with a flag, and if onConfigurationChanged() is called, you can call the Activity's onCreate() yourself. If not, onCreate() will be called only when the Activity is first instantiated

Ricardo Vieira
  • 1,676
  • 16
  • 24
  • please, read my question. I need the activity change to be handled by android. I think you dont understand my question – NullPointerException Aug 17 '16 at 10:10
  • 1
    #1 "I need to let android handle orientation changes, because i want to get the activity recreated." - By calling onConfigurationChanged and passing the config to the super() constructor, you are NOT preventing android from handling the orientation changes. It will still handle it. #2 "But also, i need to detect when a orientation change has been produced." - with that overriden method, you can have the orientation handled by android and at the same time consume the 'config' argument to check whether it was a rotation for landscape or portrait. If this is not what you meant, apologies – Ricardo Vieira Aug 17 '16 at 10:19
  • 1
    oncreate is not being called even if i call super() constructor of that method – NullPointerException Aug 17 '16 at 10:32