4

I have an Android application that we are porting over to Honeycomb/Android 3.0 and we are using Fragments in our new interface.

I have search working via the widget as shown here.

The problem is, the widget doesn't pop up any more when using Fragments. So the question is how do I get search to be used with Fragments?

Or how can I replace this line to be used with Fragments?

getActivity().onSearchRequested();
Sufian
  • 6,405
  • 16
  • 66
  • 120
SpoiledTechie.com
  • 10,515
  • 23
  • 77
  • 100

2 Answers2

2

I solved this problem using interface/callbacks.

In my MainActivity, I write 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 in the onStart() and unset it in the 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();
}
Sufian
  • 6,405
  • 16
  • 66
  • 120
0

You can't. The SearchManager is implemented to work with Activitys, not Fragments. See this post for more information.

Community
  • 1
  • 1
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250