Fire RxJava event change only when Fragment is visible in ViewPager
<p>I have an <code>EditText</code> in an <code>Activity</code> where users can type and search for things. Below the search bar, I have 2 <code>tabs</code> attached to a <code>ViewPager</code>. I have added a RxJava event change listener for the <code>EditText</code> in the 2 <code>Fragments</code> that I have for the tabs. Here is the relevant code:</p>

<pre><code>    private Subscription subscription;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_people_search, container, false);
        ButterKnife.bind(this, view);
        LogUtil.i(TAG, "onCreateView called");

        if (getActivity() != null) {
            inputSearch = (EditText) getActivity().findViewById(R.id.editText11);
        }

        return view;
    }

    @Override
    public void onResumeFragment() {
        LogUtil.i(TAG, "onResumeFragment called");
        if (subscription == null) {
            doSearch();
        }
    }

    private void doSearch() {
        LogUtil.i(TAG, "doSearch called");
        subscription = RxTextView.textChangeEvents(inputSearch)
                .debounce(400, TimeUnit.MILLISECONDS)
                .map(new Func1&lt;TextViewTextChangeEvent, String&gt;() {
                    @Override
                    public String call(TextViewTextChangeEvent textViewTextChangeEvent) {
                        return textViewTextChangeEvent.text().toString().trim();
                    }
                })
                .switchMap(new Func1&lt;String, Observable&lt;SearchUserData&gt;&gt;() {
                    @Override
                    public Observable&lt;SearchUserData&gt; call(String s) {
                        term = s;
                        if (previousTerm.equalsIgnoreCase(term) &amp;&amp; TextUtils.isEmpty(term)) {
                            //do not reset pagination variables
                        } else if (!previousTerm.equalsIgnoreCase(term) &amp;&amp; TextUtils.isEmpty(term)) {
                            min = 0;
                            max = 14;
                        } else if (previousTerm.equalsIgnoreCase(term) &amp;&amp; !TextUtils.isEmpty(term)) {
                            //do not reset pagination variables
                        } else {
                            min = 0;
                            max = 14;
                        }
                        loadMoreOrNot = true;
                        previousTerm = term;
                        DiscoverAPI discoverAPI = restAdapter.create(DiscoverAPI.class);
                        return discoverAPI.getRxPeopleSearchResult(min, max, s, "user");
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(getRxSearchObserver());
    }
</code></pre>

<p>What I want is, when I search something, the query should go to the backend only for the tab which is selected right now, not for both the tabs. How can I do this?</p>