title: Switch between views with crossfade animation
body: <p>I've wrote a small activity that is able to switch between two views. Now I am trying to add some animation (fade-in/fade-out effect). Can anybody explain me how to do that right?</p>
  
  <p>My own attempt to do this works kinda buggy (if I will click buttons very fast, my application freezes). I use code listed below:</p>
  
  <pre><code>public class WelcomeActivity extends Activity {
  private boolean isLogin = false;
  private String KEY_IS_LOGIN = "KEY_IS_LOGIN";
  private Animation anim_fadein;
  private RelativeLayout welcome, login;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      welcome = (RelativeLayout)getLayoutInflater().inflate(R.layout.activity_welcome_menu, null);
      login = (RelativeLayout)getLayoutInflater().inflate(R.layout.activity_welcome_login, null);
      anim_fadein = AnimationUtils.loadAnimation(this, R.anim.anim_fadein);
      if (savedInstanceState != null)
          isLogin = savedInstanceState.getBoolean(KEY_IS_LOGIN, false);
      if (isLogin)
          setContentView(login);
      else
          setContentView(welcome);
  }
  
  @Override
  public void onBackPressed() {
      if (isLogin) {
          setContentView(welcome);
          welcome.startAnimation(anim_fadein);
          isLogin = false;
      } else {
          super.onBackPressed();
      }
  }
  
  @Override
  public void onSaveInstanceState(Bundle outState) {
      outState.putBoolean(KEY_IS_LOGIN, isLogin);
      super.onSaveInstanceState(outState);
  }
  
  public void onButton1Click(View v) {
      setContentView(login);
      login.startAnimation(anim_fadein);
  }
  
  public void onButtonLoginClick(View v) {
      Intent i = new Intent(getApplicationContext(), MainActivity.class);
      startActivity(i);
  }
  
  public void onButtonBackClick(View v) {
      setContentView(welcome);
      welcome.startAnimation(anim_fadein);
  }
  </code></pre>
  
  <p>Animation XML file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;alpha
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromAlpha="0.0"
  android:toAlpha="1.0"
  android:duration="800" /&gt;
  </code></pre>
  
  <p>Thanks in advance!</p>