Android Screen Orientation Change
Solution 1:
The problem is that when the orientation is changed, the activity is restarted. There are multiple ways to fix that.
One way is to make it so that the activity won't restart when the orientation is changed. Here's how to do that:
Add android:configChanges="orientation|screenSize"
to your <activity
tag, which is in your AndroidManifest.xml
, like so:
<activityandroid:name="UserIdActivity"android:label="@string/app_name"android:configChanges="orientation|screenSize" />
Rastikan's answer did it slightly differently, but his way of doing it won't work for any API after API 12 (source and this too). My way above, you don't actually need to call onConfiguationChanged
in your activity class.
Another way of doing it would to use onSaveInstanceState(Bundle savedInstanceState)
like Rastikan did.
Another way of doing it would be to let the activity restart itself, but then using the onResume()
method to change the background colour, if necessary, when the activity is restarted. Like this:
publicboolean changeColor = false;
// set changeColor to be true whenever you change the background colourpublicvoidonResume()
{
if (changeColor) {
// change the background color
}
}
Solution 2:
Here is a short/alternate version assuming a view (view1) of some sort.
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
publicclassMainActivityextendsActivity {
privatestaticfinalintDEFAULT_COLOR= Color.WHITE;
privateint myColor;
// Assuming a 'view' of some sortprivate View myView;
// Declare UI elementsprivate Button firstButton;
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Our only layout for this app// is main.xml
myView = (View) findViewById( R.id.view1 );
firstButton = (Button) findViewById( R.id.button1 );
Log.i("MainActivity", "onCreate"
+ ((null == savedInstanceState) ? "(null)"
: "(savedInstanceState)"));
if (savedInstanceState != null) {
myColor = savedInstanceState.getInt("COLOR");
} else {
myColor = DEFAULT_COLOR;
}
myView.setBackgroundColor(myColor);
firstButton.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
if (myColor == Color.WHITE)
myColor = Color.BLACK;
elseif (myColor == Color.BLACK)
myColor = Color.WHITE;
myView.setBackgroundColor(myColor);
}
});
}
@Override// this method is called before android trashes and recreates your activityprotectedvoidonSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("COLOR", myColor);
}
}
Alternatively you could do it the cheap-dumb-ass way: Locking the screen. add the 'android:configChanges' attribute to your activity.
<activityandroid:name="UserIdActivity"android:label="@string/app_name"android:configChanges="orientation" />
And implement the 'onConfigurationChanged' callback to do nothing
publicvoidonConfigurationChanged( Configuration newConfig){
}
Post a Comment for "Android Screen Orientation Change"