Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem is that when the orientation is changed, the activity is restarted. There are multiple ways to fix that. </p> <p>One way is to make it so that the activity <em>won't</em> restart when the orientation is changed. Here's how to do that:</p> <p>Add <code>android:configChanges="orientation|screenSize"</code> to your <code>&lt;activity</code> tag, which is in your <code>AndroidManifest.xml</code>, like so:</p> <pre><code>&lt;activity android:name="UserIdActivity" android:label="@string/app_name" android:configChanges="orientation|screenSize" /&gt; </code></pre> <p><a href="https://stackoverflow.com/questions/20306809/android-screen-orientation-change/20306937#20306937">Rastikan's answer</a> did it slightly differently, but his way of doing it won't work for any API after API 12 (<a href="https://stackoverflow.com/questions/5620033/onconfigurationchanged-not-getting-called">source</a> and <a href="http://developer.android.com/guide/topics/resources/runtime-changes.html" rel="nofollow noreferrer">this too</a>). My way above, you don't actually need to call <code>onConfiguationChanged</code> in your activity class.</p> <p>Another way of doing it would to use <code>onSaveInstanceState(Bundle savedInstanceState)</code> like <a href="https://stackoverflow.com/users/710183/rastikan">Rastikan</a> did.</p> <p>Another way of doing it would be to let the activity restart itself, but then using the <code>onResume()</code> method to change the background colour, if necessary, when the activity is restarted. Like this:</p> <pre><code>public boolean changeColor = false; // set changeColor to be true whenever you change the background colour public void onResume() { if (changeColor) { // change the background color } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload