Viewpager with a custom ListFragment (including a searchbar) disables the searchbar after switching tab
<p>I have ViewPager with a custom ListFragment (including a searchbar(realised as an EditText with a TextWatcher)) in one Tab and some other ListFragments in the other tabs. As soon as I switch to a Tab outside of the standard mViewPager.setOffscreenPageLimit(x), and back to the custom Listfragment, the searchbar does not work anymore. My current solution to solve this problem is to set the mViewPager.setOffscreenPageLimit() high enough, but there must be a better way to solve the problem. </p>

<pre><code>public class AllServicesFragment extends ListFragment{
private static final String TAG = "AllServicesFragment";
EditText searchBar = null;
private ServiceListAdapter mAdapter;

private ArrayList&lt;String&gt; mItems;
private LayoutInflater mInflater;
private TextWatcher filterTextWatcher = new TextWatcher() {

    public void afterTextChanged(Editable s) {
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before, int count)     {
        Log.d("AllServicesFragment - Textwatcher", "CharSequence: " + s);
        mAdapter.getFilter().filter(s);
        mAdapter.notifyDataSetChanged();
    }

};
    private static final String[] NUMBERS = {... Teststrings ...};

public AllServicesFragment() {
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setRetainInstance(true);
    mInflater = LayoutInflater.from(getActivity());
    searchBar = (EditText) this.getActivity().findViewById(R.id.searchfield);
    if (mAdapter == null) {
        mItems = new ArrayList&lt;String&gt;();
        for(String s : NUMBERS)
        {
            mItems.add(s);
        }
        mPosition = NUMBERS.length;
        mAdapter = new ServiceListAdapter(getActivity(), mItems, true);
    }

    getListView().setAdapter(mAdapter);

    searchBar.addTextChangedListener(filterTextWatcher);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
    View tv = v.findViewById(R.id.searchfield);
    ((EditText) tv).setHint("AllServicesFragment");
    return v;
}
}

public class ServiceListAdapter extends ArrayAdapter&lt;String&gt; implements SectionIndexer {
public ArrayList&lt;String&gt; items;
public ArrayList&lt;String&gt; filtered;
private Context context;
private HashMap&lt;String, Integer&gt; alphaIndexer;
private String[] sections = new String[0];
private Filter filter;
private boolean enableSections;

private String TAG = "ServiceListAdapter";
public String getByPosition(int position) {
    return items.get(position);
}

public ServiceListAdapter(Context context, ArrayList&lt;String&gt; items, boolean enableSections) {
    super(context, 0, (ArrayList&lt;String&gt;)items.clone());

    this.filtered = items;
    this.items = items;

    this.context = context;
    this.filter = new ServiceFilter();
    this.enableSections = enableSections;

    if (enableSections) {
        alphaIndexer = new HashMap&lt;String, Integer&gt;();
        for (int i = items.size() - 1; i &gt;= 0; i--) {
            String element = items.get(i);
            String firstChar = element.substring(0, 1).toUpperCase();
            if (firstChar.charAt(0) &gt; 'Z' || firstChar.charAt(0) &lt; 'A')
                firstChar = "@";
            alphaIndexer.put(firstChar, i);
        }

        Set&lt;String&gt; keys = alphaIndexer.keySet();
        Iterator&lt;String&gt; it = keys.iterator();
        ArrayList&lt;String&gt; keyList = new ArrayList&lt;String&gt;();
        while (it.hasNext())
            keyList.add(it.next());

        Collections.sort(keyList);
        sections = new String[keyList.size()];
        keyList.toArray(sections);
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)   context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.servicelist_item_combined, null);
    }

    String o = filtered.get(position);
    if (o != null) {
        TextView tt = (TextView) v.findViewById(R.id.list_item_entry_title);
        TextView bt = (TextView) v.findViewById(R.id.list_item_entry_summary);
        if (tt != null)
            tt.setText(o);
        if (bt != null)
            bt.setText("Text");

        if (enableSections &amp;&amp; getSectionForPosition(position) != getSectionForPosition(position + 1)) {
            TextView h = (TextView) v.findViewById(R.id.list_item_section_text);
            h.setText(sections[getSectionForPosition(position)]);
            h.setVisibility(View.VISIBLE);
        } else {
            TextView h = (TextView) v.findViewById(R.id.list_item_section_text);
            h.setVisibility(View.GONE);
        }
    }

    return v;
}

@Override
public void notifyDataSetInvalidated() {
    if (enableSections) {
        for (int i = items.size() - 1; i &gt;= 0; i--) {
            String element = items.get(i);
            String firstChar = element.substring(0, 1).toUpperCase();
            if (firstChar.charAt(0) &gt; 'Z' || firstChar.charAt(0) &lt; 'A')
                firstChar = "@";
            alphaIndexer.put(firstChar, i);
        }

        Set&lt;String&gt; keys = alphaIndexer.keySet();
        Iterator&lt;String&gt; it = keys.iterator();
        ArrayList&lt;String&gt; keyList = new ArrayList&lt;String&gt;();
        while (it.hasNext()) {
            keyList.add(it.next());
        }

        Collections.sort(keyList);
        sections = new String[keyList.size()];
        keyList.toArray(sections);

        super.notifyDataSetInvalidated();
    }
}

public int getPositionForSection(int section) {
    if (!enableSections)
        return 0;
    String letter = sections[section];

    return alphaIndexer.get(letter);
}

public int getSectionForPosition(int position) {
    if (!enableSections)
        return 0;
    int prevIndex = 0;
    for (int i = 0; i &lt; sections.length; i++) {
        if (getPositionForSection(i) &gt; position &amp;&amp; prevIndex &lt;= position) {
            prevIndex = i;
            break;
        }
        prevIndex = i;
    }
    return prevIndex;
}

public Object[] getSections() {
    return sections;
}

@Override
public Filter getFilter() {
    if (filter == null)
        filter = new ServiceFilter();
    return filter;
}

private class ServiceFilter extends Filter {

    private String TAG = "ServiceFilter";
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        constraint = constraint.toString().toLowerCase();
        FilterResults result = new FilterResults();
        if (constraint != null &amp;&amp; constraint.toString().length() &gt; 0) {
            ArrayList&lt;String&gt; filt = new ArrayList&lt;String&gt;();
            ArrayList&lt;String&gt; lItems = new ArrayList&lt;String&gt;();
            synchronized (this) {
                Log.d(TAG, "Itemsanzahl: " + items.size());
                lItems.addAll(items);
            }
            for (int i = 0, l = lItems.size(); i &lt; l; i++) {
                String m = lItems.get(i);
                if (m.toLowerCase().contains(constraint))
                    filt.add(m);
            }
            result.count = filt.size();
            result.values = filt;
        } else {
            synchronized (this) {
                result.values = items.clone();
                result.count = items.size();
            }
        }
        return result;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        filtered = (ArrayList&lt;String&gt;) results.values;
        notifyDataSetChanged();
        clear();
        for (int i = 0, l = filtered.size(); i &lt; l; i++)
            add(filtered.get(i));
        notifyDataSetInvalidated();
    }

}
}

public class TabsAdapter extends FragmentPagerAdapter
implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList&lt;TabInfo&gt; mTabs = new ArrayList&lt;TabInfo&gt;();

    static final class TabInfo {
        private final Class&lt;?&gt; clss;
        private final Bundle args;

        TabInfo(Class&lt;?&gt; _class, Bundle _args) {
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(FragmentActivity activity, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mActionBar = activity.getActionBar();
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class&lt;?&gt; clss, Bundle args) {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }


    public int getCount() {
        return mTabs.size();
    }

    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(), info.args);
    }


    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }


    public void onPageSelected(int position) {
        mActionBar.setSelectedNavigationItem(position);
    }


    public void onPageScrollStateChanged(int state) {
    }


    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        mViewPager.setCurrentItem(tab.getPosition());
        Log.v("TabsAdapter", "clicked");
        Object tag = tab.getTag();
        for (int i=0; i&lt;mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);
            }
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {}

    public void onTabReselected(Tab tab, FragmentTransaction ft) {}

    public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {}

    @Override
    public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {    
        Object tag = tab.getTag();
        for (int i=0; i&lt;mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);
            }
        }
    }

    public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {}
}

public class ViewPagerTestActivity extends FragmentActivity {

private TabsAdapter mTabsAdapter;
private ViewPager mPager;
public ActionBar mActionBar;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        mPager = new ViewPager(this);
        mPager.setId(R.id.pager);

        mPager.setOffscreenPageLimit(6);// my current solution!!!

        setContentView(mPager);
        final ActionBar bar = getActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayShowTitleEnabled(false);
        bar.setDisplayShowHomeEnabled(false);

        mTabsAdapter = new TabsAdapter(this, mPager);
        mTabsAdapter.addTab(bar.newTab().setText(R.string.allservicestab),
        AllServicesFragment.class, null);

        mTabsAdapter.addTab(bar.newTab().setText("TWO"),
        ArrayListFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("THREE"),
        ArrayListFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("FOUR"),
        ArrayListFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("FIVE"),
        ArrayListFragment.class, null);

    } catch (Exception e) {
        Log.e("ViewPager", e.toString());
    }
}


public static class ArrayListFragment extends ListFragment {
    int mNum;
    static ArrayListFragment newInstance(int num) {
        ArrayListFragment f = new ArrayListFragment();
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = (getArguments() != null ? getArguments().getInt("num") : 1) + 1;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_pager_list, container,
                false);
        View tv = v.findViewById(R.id.searchfield);
        ((EditText) tv).setHint("Fragment numbers #" + mNum);
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(new ArrayAdapter&lt;String&gt;(getActivity(),
                android.R.layout.simple_list_item_1, sCheeseStrings));
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.i("FragmentList", "Item clicked: " + id);
    }
}

public static final String[] sCheeseStrings = {... Testdata ... };
</code></pre>

<p>}</p>
