title: fragment remove/add issue searchview
body: <p>I'm having some issues with a <code>searchView</code> in my <code>Fragments</code>.</p>
  
  <p>So, when I'm pressing the <code>search_icon</code> within the <code>actionbar</code>, the method <code>searchClick()</code> is executed.</p>
  
  <p><strong>menu_main.xml</strong></p>
  
  <pre><code>  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"&gt;
  &lt;item android:id="@+id/search"
      android:title="@string/app_name"
      android:icon="@drawable/ic_search"
      app:showAsAction="ifRoom|collapseActionView"
      app:actionViewClass="android.support.v7.widget.SearchView"
      android:onClick="searchClick"
      android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
      /&gt;
  &lt;/menu&gt;
  </code></pre>
  
  <p><strong>MainActivity.Java</strong></p>
  
  <pre><code>package formelhelfer5_10.formelhelfer5_10;
  
  
  import android.app.SearchManager;
  import android.content.Context;
  import android.content.pm.PackageManager;
  import android.os.Bundle;
  import android.support.design.widget.FloatingActionButton;
  import android.support.design.widget.Snackbar;
  import android.support.v4.app.ActivityCompat;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentTransaction;
  import android.support.v4.content.ContextCompat;
  import android.support.v7.app.AlertDialog;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.SearchView;
  import android.support.v7.widget.Toolbar;
  import android.view.MenuInflater;
  import android.view.View;
  import android.view.Menu;
  import android.view.MenuItem;
  
  import android.content.Intent;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class MainActivity extends AppCompatActivity implements SearchView.OnCloseListener, SearchView.OnQueryTextListener
  {
  
  
  
  
     @Override
     protected void onCreate (Bundle savedInstanceState) {
  
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);
  
         Intent sIntent = getIntent();
         if (Intent.ACTION_SEARCH.equals(sIntent.getAction())) {
             String query = sIntent.getStringExtra(SearchManager.QUERY);
  
         }
  
  
  
  
     }
      public void searchClick(MenuItem item){
          Fragment frag = new Results();
          Fragment rem = new FaecherFrag();
          FragmentTransaction ft  = getSupportFragmentManager().beginTransaction();
  
          ft.remove(rem).add(R.id.FragFaecher,frag).commit();
  
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.menu_main, menu);
  
  
          SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
          SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
  
          searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
          searchView.setIconifiedByDefault(false);
          searchView.setOnCloseListener(this);
          searchView.setOnQueryTextListener(this);
  
  
  
  
          return true;
      }
  
  
      @Override
      public boolean onClose() {
          Fragment frag = new Results();
          Fragment rem = new FaecherFrag();
          FragmentTransaction ft  = getSupportFragmentManager().beginTransaction();
          ft.remove(rem).add(R.id.results,frag).commit();
          return false;
      }
  
      @Override
      public boolean onQueryTextSubmit(String query) {
          return false;
      }
  
      @Override
      public boolean onQueryTextChange(String query) {
          TextView t = (TextView) findViewById(R.id.tv);
          if (query.isEmpty()){return false;}else
          {t.setText(query);}
  
          return false;
     }
  }
  </code></pre>
  
  <p><code>searchClick</code> then removes the current <code>Fragment</code></p>
  
  <p><strong>activity_main.xml</strong></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"
      android:id="@+id/main_content"
  tools:context="formelhelfer5_10.formelhelfer5_10.MainActivity"&gt;
  
  
  &lt;android.support.design.widget.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:theme="@style/AppTheme.AppBarOverlay"&gt;
  
      &lt;android.support.v7.widget.Toolbar
          android:id="@+id/toolbar"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          android:background="?attr/colorPrimary"
          app:popupTheme="@style/AppTheme.PopupOverlay"/&gt;
  
  &lt;/android.support.design.widget.AppBarLayout&gt;
  
  &lt;include layout="@layout/content_main" /&gt;
  
  
  &lt;/android.support.design.widget.CoordinatorLayout&gt;
  </code></pre>
  
  <p><strong>content_main.xml</strong>  </p>
  
  <pre><code>    xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/main_content"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      tools:context="formelhelfer5_10.formelhelfer5_10.MainActivity"
      tools:showIn="@layout/activity_main"
      android:background="?android:colorBackground"&gt;
  
  
     &lt;fragment
         android:id="@+id/FragFaecher"
         android:name="formelhelfer5_10.formelhelfer5_10.FaecherFrag"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
  
             /&gt;
  
  
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>FaecherFrag.Java</strong></p>
  
  <pre><code>package formelhelfer5_10.formelhelfer5_10;
  
  import android.content.Context;
  import android.content.Intent;
  import android.net.Uri;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.ListFragment;
  import android.view.LayoutInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ArrayAdapter;
  import android.widget.ListAdapter;
  import android.widget.ListView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import java.lang.reflect.Array;
  
  
  public class FaecherFrag extends ListFragment {
      sql sqlo = new sql();
  
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
          String[] m_fach = sqlo.getFaecher(getContext());
  
  
          ListAdapter adptFach =
                  new ArrayAdapter&lt;&gt;(
                          getActivity(), android.R.layout.simple_list_item_1, m_fach);
          setListAdapter(adptFach);
          //Array m_fach wird als Liste angezeigt
      }
      @Override
      public void onListItemClick(ListView l, View v, int position, long id){
          super.onListItemClick(l, v, position, id);
          transFach(position);
          //uebergibt die Position des Fachs an die naechste Methode
      }
      private void transFach(int position){
  
          String[] m_fach = sqlo.getFaecher(getContext());
          String sql_fach = m_fach[position]; //String für die SQL-Abfrage. Zeigt welches Fach ausgewaehlt wurde
          String[] m_thema = sqlo.getThemen(getContext(), sql_fach);
  
  
          Intent toThemen = new Intent(getActivity(),Themen.class);
          toThemen.putExtra("thema_key",m_thema);
          startActivity(toThemen);
  
      }
  
  
  
  
  }
  </code></pre>
  
  <p>and adds another <code>Fragment</code> that displays the query in a <code>TextView</code></p>
  
  <p><strong>fragment_results.xml</strong></p>
  
  <pre><code> &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent" android:layout_height="match_parent"
          android:id="@+id/results"
          android:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          android:background="?android:colorBackground" &gt;
  
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/tv"
              android:text="Text"
              android:textAppearance="?android:textAppearanceLarge"
              android:textStyle="bold"/&gt;
      &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>Results.Java</strong></p>
  
  <pre><code>package formelhelfer5_10.formelhelfer5_10;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  public class Results extends Fragment {
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
         View rootView = inflater.inflate(R.layout.fragment_results, container, false);
  
          return rootView;
      }
  
  }
  </code></pre>
  
  <p>My first Issue is, that when I submit the query, the second <code>Fragment</code> (<code>fragment_results</code>) "disappears" and the normal <code>ListFragment</code> (<code>activity_main</code>) appears. I don't know how to solve this.
  The second Issue is, that when I dismiss/close the <code>Search Widget</code>, I expect the <code>OnCloseListener</code> to remove the second <code>Fragment</code> (<code>fragment_results</code>) and show the <code>ListFragment</code> (<code>activity_main</code>). Although I implemented this in the <code>OnCloseListener</code>, nothing happens. Or am I confusing the <code>OnCloseListener</code> with something else?</p>
  
  <p>I would be glad, if you can help me with this.</p>