1

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.

Vinoth Vino
  • 9,166
  • 3
  • 66
  • 70
Paddy S
  • 45
  • 6
  • What is your AsyncTask code? – Nigam Patro Jul 07 '16 at 12:21
  • Make sure you're not adding a different instance of `MyFragmentClass` in the `FragmentTransaction`. Also, the code you've shared per se would give an error. You haven't closed the double quotes here: `bundle.putString("test", "From AsyncTask);` – SlashG Jul 07 '16 at 12:44
  • Thank you for your suggestions Cgx solution worked – Paddy S Jul 07 '16 at 13:50

2 Answers2

2

of course you can't get bundle ,because create fragment and get from AsyncTask is Asynchronous).so you could use BroadCastReceiver to recevie data when asyncTask done; fragment:

 @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.layout_demo, container, false);
    //to register br
    initBroadCastReceiver();
    return view;
}

private void initBroadCastReceiver() {
    manager = LocalBroadcastManager.getInstance(getContext());
    MyBroadCastReceiver receiver = new MyBroadCastReceiver();
    IntentFilter filter = new IntentFilter();
    //whatever
    filter.addAction("com.action.test");
    manager.registerReceiver(receiver,filter);
}

class MyBroadCastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        //e.g
        String key = intent.getStringExtra("key");
    }
}

asyncTask->onPostExcute()

Intent intent = new Intent("com.action.test");
    intent.putExtra("key","123");
    manager.sendBroadcast(intent);
Cgx
  • 753
  • 3
  • 6
  • Thank you for your help. Your solution worked. I just had to add manager = LocalBroadcastManager.getInstance(getApplicationContext()); to the asyncTask. – Paddy S Jul 07 '16 at 13:46
0

Try this in your onCreateView

Bundle b = getActivity().getIntent().getExtras();
    if(b != null) {
        String test = b.getString("test");
    }

this also helps to solve null pointer error

Chirag Arora
  • 816
  • 8
  • 20