title: Android Fragments should I reuse 1 fragment or create new instances?
body: <p>I am trying to learn Fragments in Android and from various examples I have found there seems to be different ways of doing it and I just wanted to get some advice as to which is the correct way, or at least under which circumstances one way should be used over another.</p>
  
  <p>One example created a layout that contained a fragment and a FrameLayout.  In the code, when an item is selected from the ListFragment a new Fragment is created (with some data it requires in the constructor) and the FrameLayout is replaced with this new Fragment (using FragmentTransaction.replace()).</p>
  
  <p>Another example has a layout file that declares the 2 fragments side by side.  Now in the code when the user selects an item from the list in one fragment a call is made to the other fragment to update the data (based on the selected item).</p>
  
  <p>So I am just wondering if either of these methods is preferred over the other or if there are certain circumstances where one should be used?</p>
  
  <p>EDIT: here is the code for each of the two methods I was referring to:</p>
  
  <p>1:</p>
  
  <pre><code>        mCurCheckPosition = index;
  
          if (mDualPane) {
              // We can display everything in-place with fragments, so update
              // the list to highlight the selected item and show the data.
              getListView().setItemChecked(index, true);
  
              // Check what fragment is currently shown, replace if needed.
              DetailsFragment details = (DetailsFragment)
                      getFragmentManager().findFragmentById(R.id.details);
              if (details == null || details.getShownIndex() != index) {
                  // Make new fragment to show this selection.
                  details = DetailsFragment.newInstance(index);
  
                  // Execute a transaction, replacing any existing fragment
                  // with this one inside the frame.
                  FragmentTransaction ft = getFragmentManager().beginTransaction();
                  ft.replace(R.id.details, details);
                  ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                  ft.commit();
              }
  
          } else {
              // Otherwise we need to launch a new activity to display
              // the dialog fragment with selected text.
              Intent intent = new Intent();
              intent.setClass(getActivity(), DetailsActivity.class);
              intent.putExtra("index", index);
              startActivity(intent);
          }
  </code></pre>
  
  <p>2:</p>
  
  <pre><code>public void onListItemClick(ListView l, View v, int position, long id) {
      String item = (String) getListAdapter().getItem(position);
      DetailFragment fragment = (DetailFragment) getFragmentManager()
              .findFragmentById(R.id.detailFragment);
      if (fragment != null &amp;&amp; fragment.isInLayout()) {
          fragment.setText(item);
      } else {
          Intent intent = new Intent(getActivity().getApplicationContext(),
                  DetailActivity.class);
          intent.putExtra("value", item);
          startActivity(intent);
  
      }
  
  }
  </code></pre>