title: Why does my layout get messed up after clearing FLAG_TRANSLUCENT_STATUS flag?
body: <p>I have a single Activity application. It displays a <code>LoginFragment</code> which makes Status and Navigation bars translucent so that it can display a background image behind it. However, after login this fragment is replaced by another fragment which needs to display the usual solid Status and Navigation bars. So, before <code>LoginFragment</code> gets removed it unsets the flags it had set, to make the bars opaque.</p>
  
  <p>The problem that I am facing is that after I login, the fragment with normal Status and Navigation bars has its Action bar displaced down. If I rotate my screen to landscape and then back to portrait to force the recreation of the layout then the Action bar snaps back to correct position.</p>
  
  <p><em>When Status bar is translucent. This is perfectly fine.</em>
  <a href="https://i.stack.imgur.com/RFLou.png" rel="noreferrer"><img src="https://i.stack.imgur.com/RFLou.png" alt="When Status bar is translucent. This is perfectly fine."></a></p>
  
  <p><em>On next screen Action bar is displaced downwards. Also notice that Status bar is black in color, it should have been gray, according to theme.</em>
  <a href="https://i.stack.imgur.com/vjYLC.png" rel="noreferrer"><img src="https://i.stack.imgur.com/vjYLC.png" alt="On next screen Action bar is displaced downwards."></a></p>
  
  <p><em>Now if I go back to first screen, the top translucent bar is fine, but the whole content is shifted upwards, leaving a blank white space below.</em>
  <a href="https://i.stack.imgur.com/DxtTv.png" rel="noreferrer"><img src="https://i.stack.imgur.com/DxtTv.png" alt="Now if I go back to first screen, the top translucent bar is fine, but the whole content is shifted upwards, leaving a blank white space below."></a></p>
  
  <p>Code in <code>LoginFragment</code> which programmatically makes the bars translucent and also restores them:-</p>
  
  <pre><code>@Override
  public void onResume() {
      super.onResume();
      if (hasTransparentStatusBar()) {
          setStatusBarTranslucent();
      }
  }
  
  @Override
  public void onPause() {
      if (hasTransparentStatusBar()) {
          setStatusBarOpaque();
      }
      super.onPause();
  }
  
  protected boolean hasTransparentStatusBar() {
      return true;
  }
  
  protected void setStatusBarTranslucent() {
      if (Build.VERSION.SDK_INT &gt;= 21) {
          Window window = getActivity().getWindow();
          window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
                  | WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                  | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
      }
  }
  
  protected void setStatusBarOpaque() {
      if (Build.VERSION.SDK_INT &gt;= 21) {
          Window window = getActivity().getWindow();
          window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
                  | WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                  | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
      }
  }
  </code></pre>
  
  <p>Layout xml of Activity:-</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/drawer_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.app.MainActivity" &gt;
  
      &lt;!-- The main content view --&gt;
  
      &lt;FrameLayout
          android:id="@+id/container"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
      &lt;!-- The navigation drawer --&gt;
  
      &lt;ListView
          android:id="@+id/left_drawer"
          android:layout_width="240dp"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:choiceMode="singleChoice"
          android:divider="@color/divider"
          android:dividerHeight="0dp"
          android:background="@color/primary"
          /&gt;
  
  &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>
  
  <p><code>LoginFragment</code>'s layout xml:-</p>
  
  <p>
  </p>
  
  <pre><code>&lt;ImageView
      android:layout_width="wrap_content"
      android:layout_height="200dp"
      android:src="@drawable/logo"
      android:id="@+id/logo"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true" /&gt;
  
  &lt;Button
      android:padding="@dimen/default_margin"
      android:text="Login using Facebook"
      android:textColor="@android:color/white"
      android:background="@drawable/com_facebook_button_background"
      android:id="@+id/authButton"
      android:layout_below="@+id/logo"
      android:layout_centerHorizontal="true"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" /&gt;
  </code></pre>
  
  <p></p>