Is it possible to search inside fragments?

I have an Android app that we port to Honeycomb / Android 3.0, and we use Fragment in our new interface.

I have a search that works through a widget, as shown here .

The problem is that the widget no longer appears when using Fragment s. So the question is, how do I get the search to use with Fragment s?

Or how can I replace this string to be used with Fragment s?

 getActivity().onSearchRequested(); 
+4
source share
2 answers

I solved this problem using interface / callbacks.

In my MainActivity I am writing a callback interface:

 private SearchRequestedCallback mSearchRequestedCallback; public void setSearchRequestedCallback(SearchRequestedCallback callback) { mSearchRequestedCallback = callback; } public interface SearchRequestedCallback { void onSearchRequested(); } 

In my Fragment , I set the callback to onStart() and disabled it in onStop() :

 @Override public void onStart() { super.onStart(); getActivity().setTitle(getResources().getString(R.string.app_name)); ((MainActivity)getActivity()).setSearchRequestedCallback(new SearchRequestedCallback() { @Override public void onSearchRequested() { addFilter(); } }); } @Override public void onStop() { ((MainActivity)getActivity()).setSearchRequestedCallback(null); super.onStop(); } 
+2
source

You can not. SearchManager implemented to work with Activity s, not Fragment s. See this post for more details.

0
source

All Articles