How to pass data from an activity to a fragment. It is well documented but unfortunately no solution works for me.
I want to send data from an inner AsyncTask class inside my main activity to another fragment.
The most popular answer is to use Bundle so I have tried the following:
AsyncTask:
Bundle bundle = new Bundle();
bundle.putString("test", "From AsyncTask");
MyFragmentClass fragobj = new MyFragmentClass();
fragobj.setArguments(bundle);
Fragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_one, container, false);
String value = getArguments().getString("test");
Log.i("valueTest", value);
return rootView;
}
The string is not being passed to the fragment as I receive the following:
"java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference"
I would appreciate any help.