Reputation: 53
I'm trying to create an android app. And I want to pick the orientation at the start of the application. But after that, I don't want the orientation to change regardless of how their phone is orientated. I know I could put android:screenorientation in the manifest file and lock the orientation but I really don't want to do this. What I want is initially, I will set the orientation to default orientation, let's say landscape. Then, the user will have one chance to change the orientation, after which the orientation will be locked. If the user doesn't pick orientation during the start of the application, then the orientation will be locked to default one. So, I just need to know how to not reload the activity on orientation change. I am not sure if I was good enough in explaining my situation. But thank you for help in advance.
Upvotes: 0
Views: 53
Reputation: 7504
To do so, first declare the activity in your manifest as follows:
<activity android:name=".StartActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
This implies that now, when the device orientation is called for this Activity, instead of Android recreating it, it will instead call the onConfigurationChanged() method from the Activity. All you have to do now is to declare that method:
public void onConfigurationChanged(Configuration newConfig) {
// Do nothing -- if that is what you really want !!
// Check the orientation of the screen if you need to do something
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();
}
}
you can read more about this at Android - Handling runtime changes
Upvotes: 1