title: SearchView with ViewPager adapter
body: <p>I have one activity with three fragments. I'm using <code>ViewPager</code> with tabs. One fragment is a <code>SearchFragment</code> with a <code>ListView</code>. It sends query to Service class and then, after the service is done it calling a custom adapter and shows the results. The problem is, that i don't know how to access the <code>SearchView</code> from the <code>SearchFragment</code>, how to get the query, that the user has entered.
  Thnx.
  Main Activity code:</p>
  
  <pre><code>@Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.menu_main, menu);
      SearchManager searchManager =
              (SearchManager) getSystemService(Context.SEARCH_SERVICE);
      searchView =
              (SearchView) menu.findItem(R.id.search).getActionView();
      searchView.setSearchableInfo(
              searchManager.getSearchableInfo(getComponentName()));
  
      SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
          @Override
          public boolean onQueryTextChange(String newText) {
              // Do something
              return true;
          }
  
          @Override
          public boolean onQueryTextSubmit(String query) {
              query = searchView.getQuery().toString();
              Log.e(TAG, "Main query is " + query);
  
              SearchFragment fragment = (SearchFragment)getSupportFragmentManager().findFragmentByTag("search");
              fragment.beginSearch(query);
  
              InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
              return true;
          }
      };
  
      searchView.setOnQueryTextListener(queryTextListener);
      return true;
  }
  </code></pre>
  
  <p>Fragment code:</p>
  
  <pre><code>public void beginSearch(String query) {
      Log.e(TAG, "got query "+ query);
      Intent intent;
      SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
      SharedPreferences.Editor editor = sp.edit();
      String search = sp.getString("search", "a");
      if (search.equals("Free search")) {
  
          if (connection == NOT_CONNECTED) {
              Toast.makeText(getActivity(), "No Internet connection. Can`t search", Toast.LENGTH_SHORT).show();
  
          } else {
              if (query.length() == 0) {
                  Toast.makeText(getActivity(), "Please enter text", Toast.LENGTH_SHORT).show();
              } else {
                  intent = new Intent(getActivity(), Service.class);
                  intent.setAction(Service.ACTION_SEARCH_BY_TEXT);
                  intent.putExtra("query", query);
                  getActivity().startService(intent);
                  swipeRefreshLayout.setRefreshing(true);
                  editor.putInt("refresh", SEARCH_BY_TEXT);
                  editor.commit();
              }
          }
      } else if (search.equals("Search nearby")) {
          if (connection == NOT_CONNECTED) {
              Toast.makeText(getActivity(), "No Internet connection. Can`t search", Toast.LENGTH_SHORT).show();
          } else {
              if (query.length() == 0) {
                  Toast.makeText(getActivity(), "Please enter text", Toast.LENGTH_SHORT).show();
              } else {
                  intent = new Intent(getActivity(), Service.class);
                  intent.setAction(Service.ACTION_SEARCH_NEARBY);
                  intent.putExtra("query", query);
                  intent.putExtra("lat", lat);
                  intent.putExtra("lng", lng);
                  Log.e(TAG, "lat" + lat + "lng" + lng);
                  getActivity().startService(intent);
                  swipeRefreshLayout.setRefreshing(true);
                  editor.putInt("refresh", SEARCH_NEARBY);
                  editor.commit();
              }
          }
      }
  }
  </code></pre>