Skip to content Skip to sidebar Skip to footer

How To Send Data From Fragment To Another Activity?

I need to send data from Fragment to another activity I am using this code in my LoadsFragment under HomeActivity Intent intent = new Intent(activity, LoadActivity.class); intent.p

Solution 1:

but intent has no Extras

Your screenshot says it does...

Your second screenshot shows processIntent(Bundle bundle), but the third shows processIntent(Intent intent), so you should clarify which isn't working. But both Bundles are not null.

Fragments have their own startActivity method. You only need the parent Activity to create the Intent

Intent intent = newIntent(getActivity(), LoadActivity.class);
intent.putExtra("loadsPosition",position);
startActivity(intent);

Most importantly, your position is an integer, but you're trying to get it as a string, therefore the string will be null

Intent intent=getIntent();    
int loadsPosition = intent.getIntExtra("loadsPosition", -1);

The Intent should not be null, and it should have a Bundle. If this integer comes back as -1, then the default here has been returned, and you should debug some more with a smaller example

Solution 2:

Try using interfaces.

Any fragment that should pass data back to its HomeActivity should declare an interface to handle and pass the data. Then make sure your HomeActivity implements those interfaces. For example:

In your fragment, declare the interface...

publicinterfaceOnDataPass {
    publicvoidonDataPass(String data);
}

Then, connect the HomeActivity class' implementation of the interface to the fragment in the onAttach method, like so:

OnDataPass dataPasser;

@OverridepublicvoidonAttach(Context context) {
    super.onAttach(context);
    dataPasser = (OnDataPass) context;
}

Within your fragment, when you need to handle the passing of data, just call it on the dataPasser object:

publicvoidpassData(String data) {
    dataPasser.onDataPass(data);
}

Finally, in your HomeActivity which implements OnDataPass...

@OverridepublicvoidonDataPass(String data) {
    Log.d("LOG","hello " + data);
}

Solution 3:

In fragment Use

Intent intent = newIntent(getActivity, LoadActivity.class);
intent.putExtra("loadsPosition",position);
activity.startActivity(intent);

in LoadActivity

StringloadsPosition= getIntent().getStringExtra("loadsPosition");

Solution 4:

Can you try like this and see if you're getting the extras:

  1. When you create the intent: Intent intent = new Intent(this, LoadActivity.class);

  2. But most probably you are processing getExtras() instead of the intent itself.

    try{ processIntent(getIntent()); }catch(JSONException e){ ...

Solution 5:

You did not attach extras to your intent:

Intentintent=newIntent(activity.this, LoadActivity.class);
Bundlebundle=newBundle();
bundle.putInt("loadsPosition",pos);
intent.putExtras(bundle);
activity.startActivity(intent);

or:

Intent intent = newIntent(activity.this, LoadActivity.class);
intent.putExtra("loadsPosition",pos);
activity.startActivity(intent);

Post a Comment for "How To Send Data From Fragment To Another Activity?"