The less-known over screen orientation
I’ve just found a really interesting problem today at work, it’s about screen orientation.
I was trying to create a very simple application, which:
+ contains one button to change screen orientation state: PORTRAIT to LANDSCAPE, and vice versa.
+ the text on button determines the action going to be done if click. If the current is PORTRAIT then the text should be LANDSCAPE, and vice versa.
OK, so here is one very simple implementation, easy-read, easy-understood:
package pete.android.study; import android.app.Activity; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { // the constant strings defining text to display on button static final String ORIENT_PORTRAIT = "Set Portrait"; static final String ORIENT_LANDSCAPE = "Set Landscape"; // determine screen orientation state changed boolean mState = false; // the main button Button mBtnSet = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // load button from layout mBtnSet = (Button)findViewById(R.id.btnSet); // set default text display mBtnSet.setText(ORIENT_LANDSCAPE); // handle click event mBtnSet.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // state FALSE: switch to LANDSCAPE if(!mState) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); mBtnSet.setText(ORIENT_PORTRAIT); } // state TRUE: switch to PORTRAIT else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); mBtnSet.setText(ORIENT_LANDSCAPE); } // update state mState = !mState; } }); } }
However, there has a problem inside. First, try to build and run it.
By default, the screen is set as PORTRAIT, and the text is “Set Landscape“, which is correct.
Next, try to click on button, this will happen:
The screen orientation is changed to LANDSCAPE, however, the text on button doesn’t change at all. That’s so surprising!
And keep clicking on button one more time:
Now, the text changes to “Set Portrait“, well, the orientation doesn’t change, but on the third click, the screen interface backs to the first initial state.
It’s interesting, isn’t it?
The question in mind is why it is…
First, reading this one on Google Android References – Activity#ConfigurationChanges
Reading clearly, you’ll find that, everytime the screen orientation changes, the Activity is auto destroyed and recreated again, which means the text “Set Landscape” is reset as the initial state, a re-call of onCreate() method.
That’s the problem!
The solution is very simple, by adding the attribute – android:configChanges=”orientation” – , it will avoid re-create the activity and keep the current state.
Note: if you always to handle screen orientation without attribute of handling orientation, sometimes you might get exception, or errors in your application as well, even crash-out and freeze. So always keep this in mind :).
Hope you having fun with this little tips!
Cheers,
Pete Houston