-4

I want to get search result from search bar which is in Activity and show all list in a fragment. How can I do it?

enter image description here

xskxzr
  • 12,442
  • 12
  • 37
  • 77
Rasel Khan
  • 2,976
  • 19
  • 25
  • 1
    declare an interface in your activity and then implement it on your fragment ...shouldn't be hard. – himel Mar 04 '18 at 05:32
  • 4
    Possible duplicate of [Pass ActionBar search query to fragment](https://stackoverflow.com/questions/20370969/pass-actionbar-search-query-to-fragment) – Dr.jacky Mar 04 '18 at 05:35

2 Answers2

1
@Override
public boolean onQueryTextSubmit(String query) {
    if (query.length() > 0) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        Fragment newFragment = new SearchFragment(); //your search fragment
        Bundle args = new Bundle();
        args.putString("query_string", query);
        newFragment.setArguments(args);

        transaction.replace(R.id.content_frame, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();   
    }
    return false;
}
z85510
  • 33
  • 1
  • 12
1

Hope this will solve your issue ... Let me know in the comment ...(its just an example.. as you have not provided the sufficient information related your query)

Link

I searched your query and found above link ...

Edited:

Step 1: Passing the data from activity to fragment,

Bundle bundle = new Bundle();
bundle.putString("params", "My String data");
// set MyFragment Arguments
MyFragment myObj = new MyFragment();
myObj.setArguments(bundle);

Step 2: Receiving the data to the fragment,

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString("params");
        }
    }