title: How do I keep a floating action button consistent across multiple activities?
body: <p>I am building an app that has a floating action button that opens a menu. I want to be able to keep the menu consistent without having to add it to every activity.</p>
  
  <p>I'm thinking that I could simply create the MainActivity and place a ViewStub within a LinearLayout with the id "holder". Then, when a menu button is clicked, I'll just remove the view from the holder and add the new view.</p>
  
  <p>Are there any pitfalls to this method?  Any suggestions on a better way?</p>
  
  <p>Here's my MainActivity:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      private LinearLayout holder;
      private ViewStub viewStub;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          holder = (LinearLayout) findViewById(R.id.holder);
  
          viewStub = (ViewStub) findViewById(R.id.viewStub);
          viewStub.setLayoutResource(R.layout.content_main);
          viewStub.inflate();
  
          FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
          fab.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                          .setAction("Action", null).show();
              }
          });
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          switch(id){
              case R.id.action_main:
                  loadLayout(R.layout.content_main);
                  break;
              case R.id.action_secondary:
                  loadLayout(R.layout.content_secondary);
                  break;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
      private void loadLayout(int layout){
          holder.removeAllViews();
          holder.addView(LayoutInflater.from(this).inflate(layout,holder,false));
      }
  }
  </code></pre>
  
  <p>And here's my main layout where I load in my content layouts into the "holder":</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.design.widget.CoordinatorLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      tools:context="com.example.android.simplepopoutmenu.MainActivity"&gt;
  
      &lt;LinearLayout
          android:id="@+id/holder"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"&gt;
          &lt;ViewStub
              android:id="@+id/viewStub"
              android:inflatedId="@+id/main_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent" /&gt;
      &lt;/LinearLayout&gt;
  
  
      &lt;android.support.design.widget.FloatingActionButton
          android:id="@+id/fab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="bottom|start"
          android:layout_margin="@dimen/fab_margin"
          app:srcCompat="@android:drawable/ic_menu_view" /&gt;
  
  &lt;/android.support.design.widget.CoordinatorLayout&gt;
  </code></pre>