title: Replace ListFragment
body: <p>I am using a ListFragment for displaying a list from a database in my activity. I have included a search function. Unfortunately the "old" ListFragments seem to remain in the background and the ListFragments containing the result of the query are displayed on top of it. How can I avoid, that the old ListFragments are displayed?</p>
  
  <p>My FragmentActivity:</p>
  
  <pre><code>private Button buttonSearch; 
      private TextView searchString; 
      public static String search = null; 
      static IShoppinglist shoppinglistManager;
      static IAktionen aktionenManager; 
      private AktionenListListFragment listFragment;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          Log.d(TAG, "ListFragmentActivity created"); 
          super.onCreate(savedInstanceState);
          setContentView(R.layout.articlelist);
  
          shoppinglistManager = new Shoppinglist(this);
          aktionenManager = new Aktionen(this); 
  
          buttonSearch = (Button) findViewById(R.id.search_Button);
          buttonSearch.setOnClickListener(searchListAktionen);
  
          //show all entries on start
          listFragment = new AktionenListListFragment();
          getSupportFragmentManager().beginTransaction().replace(R.id.fragment_articlelist, listFragment).commit();
      }   
  
      OnClickListener searchListAktionen = new OnClickListener() {
          public void onClick(View v) {
  
  
              try{
                  searchString = (TextView) findViewById(R.id.input_search_bezeichnung); 
                  search = searchString.getText().toString().trim();
                  Log.d(TAG, "search Button clicked "+search); 
  
                  if(search.trim().length()==0){
                      search=null;
                  }
  
                  //show all entries on start
                  listFragment = new AktionenListListFragment();
                  getSupportFragmentManager().beginTransaction().replace(R.id.fragment_articlelist, listFragment).commit();
  
              }catch(Exception ex){
                  ex.printStackTrace();
              }
          }
      };
  </code></pre>
  
  <p>Thanks in advance, </p>
  
  <p>update: </p>
  
  <p>thank you for your answers. I tried to implement them, but the main problem seems to be nowthat the onCreate and onActivityCreated method in the ListFragment are called twice (as I can see in my log messages). </p>
  
  <p>my new code: </p>
  
  <pre><code>@Override
      public void onCreate(Bundle savedInstanceState) {
          Log.d(TAG, "ListFragmentActivity created"); 
          super.onCreate(savedInstanceState);
  
          //force commit
          getSupportFragmentManager().executePendingTransactions();
  
  
          if(getSupportFragmentManager().findFragmentByTag(tag) == null) {
              setContentView(R.layout.articlelist);
  
              shoppinglistManager = new Shoppinglist(this);
              aktionenManager = new Aktionen(this); 
  
              buttonSearch = (Button) findViewById(R.id.search_Button);
              buttonSearch.setOnClickListener(searchListAktionen);
              listFragment = new AktionenListListFragment();
              getSupportFragmentManager().beginTransaction().replace(R.id.fragment_articlelist, listFragment,tag).commit();
          }else{
              Log.d(TAG, "ListFragment already exists"); 
          }
      }
  </code></pre>
  
  <p>I tried to set a unique tag for my ListFragment but this solution does not work. </p>
  
  <p>I guess that one of the ListFragments is displayed in the background and the other is updated. </p>