Reputation: 12403
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
Upvotes: 2
Views: 18012
Reputation: 110
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");
Upvotes: -1
Reputation: 1004
You can use Local Broad Cast Receiver or Interface to pass data from fragment to activity.
Upvotes: 0
Reputation: 911
In fragment Use
Intent intent = new Intent(getActivity, LoadActivity.class);
intent.putExtra("loadsPosition",position);
activity.startActivity(intent);
in LoadActivity
String loadsPosition = getIntent().getStringExtra("loadsPosition");
Upvotes: 1
Reputation: 6389
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);
Upvotes: 0
Reputation: 2734
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);
}
Upvotes: 3
Reputation: 191701
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
Upvotes: 18
Reputation: 31
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");
Upvotes: -1
Reputation: 317
Can you try like this and see if you're getting the extras:
When you create the intent:
Intent intent = new Intent(this, LoadActivity.class);
But most probably you are processing getExtras() instead of the intent itself.
try{
processIntent(getIntent());
}catch(JSONException e){
...
Upvotes: 0