How to send data from fragment to another activity?
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 = 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
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...
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);
}
Solution 3
In fragment Use
Intent intent = new Intent(getActivity, LoadActivity.class);
intent.putExtra("loadsPosition",position);
activity.startActivity(intent);
in LoadActivity
String loadsPosition = getIntent().getStringExtra("loadsPosition");
Related videos on Youtube
Comments
-
Dhanu K almost 2 years
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
-
Sharath kumar over 6 yearsquestion is unclear
-
KuLdip PaTel over 6 years
getActivity()
andactivity.startActivity(intent)
both are same? -
Dhanu K over 6 yearsYes, both are same
-
Michael Meyer over 6 yearsplease read the following post stackoverflow.com/questions/5265913/…
-
Umair over 6 yearsPossible duplicate of How to use putExtra() and getExtra() for string data
-
Dhanu K over 6 yearsplese see my updated question with screenshoots
-
OneCricketeer over 6 yearsPut a minimal reproducible example of all the code, not screenshots or out of context snippets
-
Sangeet Suresh over 6 years@DhanuK can you please post manifest block of LoadActivity
-
Asharali V U over 6 yearsChange intent.hasExtras("meta_data") to intent.hasExtras(""loadsPosition") in LoadActivity. Looks like you are checking a wrong key
-
SadeghAlavizadeh over 6 yearsYou can use
EventBus
github.com/greenrobot/EventBus for these works. -
OneCricketeer over 6 years@Dhanu Please accept an answer using the checkmark next to a post. (You should go back over old questions and do that too)
-
-
Dhanu K over 6 yearsplease see screenshots
-
OneCricketeer over 6 years
new Intent(this,
doesn't work from a Fragment class -
Dhanu K over 6 yearsdo you understood what question is? i am trying to send data to a activity, that is started in fragment.