0

I have a search view on my Main Activity, i want to pass the submitted string from MainActivity to SearchFragment.

@Override
public boolean onQueryTextSubmit(String query) {
   Bundle bundle = new Bundle();
   bundle.putString("searchTitle", query);
   SearchFragment searchFragment = new SearchFragment();
   searchFragment.setArguments(bundle);

   Intent mIntent = new Intent(MainActivity.this, SearchActivity.class);
   startActivity(mIntent);

   return true;
}

But i get NullPointerException when i try to get the data in the SearchFragment

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    strText = getArguments().getString("searchTitle");

    return inflater.inflate(R.layout.fragment_movies, container, false);
}

How can i fix this ?

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
  • 2
    https://stackoverflow.com/questions/7149802/how-to-transfer-some-data-to-another-fragment ? – P.Juni Jan 31 '20 at 08:25
  • Does this answer your question? [How to transfer some data to another Fragment?](https://stackoverflow.com/questions/7149802/how-to-transfer-some-data-to-another-fragment) – P.Juni Jan 31 '20 at 08:28
  • @P.Juni No, it always return a null value – Robin Logstride Jan 31 '20 at 08:39
  • I think you should add data to your Intent from MainActivity to SearchActivity. Then if you get data in SearchActivity you should add data to bundle and create SearchFragment with bundle. – Hello World Jan 31 '20 at 08:54

2 Answers2

0

Please try this.

In your MainActivity.

@Override
public boolean onQueryTextSubmit(String query) {
    Bundle bundle = new Bundle();
    bundle.putString("searchTitle", query);
    SearchFragment searchFragment = new SearchFragment();
    searchFragment.setArguments(bundle);

    Intent mIntent = new Intent(MainActivity.this, SearchActivity.class);

    // Add this line.
    mIntent.putExtra("key", "value");

    startActivity(mIntent);
    return true;
}

In your SearchActivity.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView([layout_name])

    Fragment fragment = new Fragment();
    Bundle bundle = new Bundle();

    // Put data respected to your data type.
    bundle.putString("key", getIntent().getStringExtra("key"));
    fragment.setArguments(bundle);

    // Add new fragment
    getSupportFragmentManager()
        .beginTransaction()
        .add([container_id], fragment)
        .addToBackStack(null)
        .commit()
}

In your Fragment.

@Override
public onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    String value = getArguments().getString("key");
}
Hello World
  • 740
  • 5
  • 18
0

If you want to pass data through bundle, at least you should put this bundle in your intent

mIntent.putExtras(bundle)

Than you can get it through this

getActivity().getIntent().getExtras().getString("searchTitle")
i30mb1
  • 3,894
  • 3
  • 18
  • 34