i want to do_something()
whenever the device rotates from Portrait to Landscape.
i have added <activity android:configChanges="orientation" >
in my manifest. So onConfigurationChanged()
will be called whenever i rotate my device. And it will not re-create the Activity.
In my onConfigurationChanged()
function:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
do_something();
}
then_fire_off_the_screen_rotation_as_normal();
// which means re-start the Activity
// i expect it will fire off onSaveInstanceState() -> onPause() -> onStop() -> onDestroy() -> onCreate() -> onStart() -> onRestoreInstanceState() -> onResume()
}
My problem is, i dont know what can i do in then_fire_off_the_screen_rotation_as_normal()
.
i have tried using setRequestedOrientation()
but it seems it is not the function serving this purpose:
When i pass in ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
, ActivityInfo.SCREEN_ORIENTATION_SENSOR
, or ActivityInfo.SCREEN_ORIENTATION_USER
, the Activity is still not re-started. (Just like not calling.)
When i pass in ActivityInfo.SCREEN_ORIENTATION_NOSENSOR
, the orientation sensor turned off. (As it said.)
When i pass in ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
, the Activity was Force Close with AndroidRuntime:NullPointerException
.
(Be specific: It is because there are programmatically added Fragments in the Portrait layout, but not in the Landscape layout. In the do_something()
function, i will remove those Fragments, as they are not needed in Landscape mode.)