android - onConfigurationChanged not called for second time -
i using following code change activity orientation while user rotates device. works fine first time, not called again. please correct me.
@override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); log.d("activity","changing orientation"); //no i18n if (newconfig.orientation == configuration.orientation_portrait) { log.d(tag, "orientation changed portrait"); // no i18n setrequestedorientation(configuration.orientation_portrait); } else if (newconfig.orientation == configuration.orientation_landscape) { log.d(tag, "orientation changed landscape"); // no i18n setrequestedorientation(configuration.orientation_landscape); } } @override protected void onrestoreinstancestate(bundle savedinstancestate) { log.d("activity","on restore instancestate"); //no i18n super.onrestoreinstancestate(savedinstancestate); } @override protected void onsaveinstancestate(bundle outstate) { log.d("activity","on save instancestate"); //no i18n super.onsaveinstancestate(outstate); }
after use setrequestedorientation(...); no longer have privilege of getting rotation notifications.
if want perform special actions when rotations occur, may want use orientationeventlistener
edit:
caution: code takes portrait 80-90 deg , >= 320, can changed.
may need run setrequestedorientation on ui thread if piece of code isn't running there already.
you should this:
morientationlistener = new orientationeventlistener(this, sensormanager.sensor_delay_ui) { @override public void onorientationchanged(int rotation) { boolean changed = false; if (((rotation >= 0) && (rotation <= 80)) || (rotation >= 320)) { setrequestedorientation(configuration.orientation_portrait); } else { setrequestedorientation(configuration.orientation_landscape); } } }; morientationlistener.enable();
Comments
Post a Comment