6

I have an app which shows fullscreen bitmaps in an activity. In order to provide fast loading time, I load them in the memory. But when the screen changes orientation, I would like to clear the cache in order to fill it again with bitmaps that fit inside the new dimensions. The only problem is that in order to do this, I need to detect when an orientation change occurs. Do anyone know how to detect this?

Quentin Golsteyn
  • 403
  • 1
  • 5
  • 12

4 Answers4

23

See the official documentation http://developer.android.com/guide/topics/resources/runtime-changes.html

Changing it will actually create a new view and onCreate will be called again.

Furthermore you can check it via

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}
noone
  • 19,520
  • 5
  • 61
  • 76
  • 6
    @noone: Note that this method will ONLY be called if you have selected configurations you would like to handle with the android.R.attr#configChanges attribute in your manifest. See javadoc! – user504342 Sep 26 '13 at 11:42
5

You can check the onSavedInstanceState from your onCreate method, if it is not null means this is configuration change.

Eugene
  • 59,186
  • 91
  • 226
  • 333
4

Another approach is using OrientationEventListener.

It can be used like this:

 OrientationEventListener mOrientationEventListener = new OrientationEventListener(
            this, SensorManager.SENSOR_DELAY_NORMAL) {

        @Override
        public void onOrientationChanged(int orientation) {
            //checking if device was rotated
            if (orientationPortrait != isPortrait(orientation)) {
                orientationPortrait = !orientationPortrait;
                Log.d(TAG, "Device was rotated!");
            }
        }
    };

To check orientation:

private boolean isPortrait(int orientation) {
    return (orientation >= (360 - 90) && orientation <= 360) || (orientation >= 0 && orientation <= 90);
}

And don't forget to enable and disable listener:

if (mOrientationEventListener != null) {
        mOrientationEventListener.enable();
    }

if (mOrientationEventListener != null) {
        mOrientationEventListener.disable();
    }
Bogdan Ustyak
  • 5,639
  • 2
  • 21
  • 16
  • I think configuration change is one thing, in this case onConfigurationChanged is the way to go. But if you have locked orientation changes and still want to get notified, orientation listeners are the best approach. Thanks Bogdan. – Murciegalo84 Nov 07 '17 at 19:53
2

Usually Orientation change calls OnCreate() unless you have done something to make it do otherwise.

You can put the logic there.

sealz
  • 5,348
  • 5
  • 40
  • 70
  • Of course makes sense, but how to make sure that I do not reload the cache each time onCreate is called? onCreate could be called simply after the activity being recreated after a phone call by example and the cache should not be reloaded in this situation. – Quentin Golsteyn Aug 19 '13 at 19:43
  • 1
    @Poplitou see siik's answer about `onSavedInstanceState` and also http://stackoverflow.com/questions/4096169/onsaveinstancestate-and-onrestoreinstancestate – sealz Aug 19 '13 at 19:54