2

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.putExtra("loadsPosition",position);
activity.startActivity(intent);

in another activity(LoadActivity) to receive data

Intent intent=getIntent();    
String loadsPosition = intent.getStringExtra("loadsPosition");

but intent has no Extras

see the screenshots below

From fragment sending intentin second activity getting extrasenter image description here

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Dhanu K
  • 11,288
  • 6
  • 24
  • 38

8 Answers8

18

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 = new Intent(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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
3

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...

public interface OnDataPass {
    public void onDataPass(String data);
}

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

OnDataPass dataPasser;

@Override
public void onAttach(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:

public void passData(String data) {
    dataPasser.onDataPass(data);
}

Finally, in your HomeActivity which implements OnDataPass...

@Override
public void onDataPass(String data) {
    Log.d("LOG","hello " + data);
}
Dilip
  • 2,622
  • 1
  • 20
  • 27
  • 1
    do you understood what question is? i am trying to send data to a activity, that is started in fragment. – Dhanu K Nov 29 '17 at 12:12
1

In fragment Use

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

in LoadActivity

String loadsPosition = getIntent().getStringExtra("loadsPosition");
Kapil Parmar
  • 881
  • 8
  • 19
0

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){ ...

fhery021
  • 317
  • 2
  • 7
0

You did not attach extras to your intent:

Intent intent = new Intent(activity.this, LoadActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("loadsPosition",pos);
intent.putExtras(bundle);
activity.startActivity(intent);

or:

Intent intent = new Intent(activity.this, LoadActivity.class);
intent.putExtra("loadsPosition",pos);
activity.startActivity(intent);
Mahdi
  • 6,139
  • 9
  • 57
  • 109
0

You can use Local Broad Cast Receiver or Interface to pass data from fragment to activity.

Rehan Sarwar
  • 994
  • 8
  • 20
-1

Actvity 1 Fragment :

 Intent intent = new Intent(getActivity(), SecondActivity.class);
 intent.putExtra("message", data); 
 startActivity(intent);

In second activity fragment retrieve in this manner

 SecondActivity activity = (SecondActivity) getActivity();
 String data= activity.getIntent().getExtras().getString("message");
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Niranjan
  • 21
  • 5
-1

You can also directly take intent extras data of activity in fragment. If you are having data in your activity intent.Use below code in your fragment:

Intent intent = getIntent().getStringExtra("position");
Kalpana Aneyrao
  • 100
  • 1
  • 11