title: How to set layout dynamically in android
body: <p>Well, Suppose there is an Activity called <strong>MainActivity</strong> and there are two layouts called <strong>layout1</strong> and <strong>layout2</strong> both have few buttons. By default <strong>MainActivity</strong> layout is <strong>layout1</strong> like following:</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.layout1);
  </code></pre>
  
  <p>Now what I did actually is by clicking a button in <strong>layout1</strong> the second layout is set like following:</p>
  
  <pre><code>    someBtn1.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              setContentView(R.layout.layout2);
          }
      });
  </code></pre>
  
  <p>There are another button in <strong>layout2</strong> to return back to <strong>layout1</strong> like following:</p>
  
  <pre><code>    someBtn2.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              setContentView(R.layout.layout1);
          }
      });
  </code></pre>
  
  <p>Problem is when I returned back to <strong>layout1</strong> then  OnClickListener of <strong>someBtn1</strong> is not working. It seems I need to set OnClickListener again for <strong>someBtn1</strong> of <strong>layout1</strong>.
  How I can write code to make them work perfectly with best practices ? </p>