I have multiple fragments in my app. I want to add search function to may app. When i add search view in toolbar, the listener is in MainActivity. The problem is when i submit the query, the action for every fragment is same. Can i differentiate the action for every fragment ? How to do that?
First of all, I guess you have added the SearchView
in Activity through menu.
If so then you can implement the menu inside each fragment instead of Activity. Then you can get Search functionality for each fragment.
To enable Option Menu
for fragment you have to override onCreate
like below:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Then you have to inflate your menu in
onCreateOptionsMenu
like below:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
super.onCreateOptionsMenu(menu, inflater);
}