How to send data from fragment to another activity?

14,777

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");
Share:
14,777

Related videos on Youtube

Dhanu K
Author by

Dhanu K

https://www.linkedin.com/in/bewithdhanu/

Updated on June 04, 2022

Comments

  • Dhanu K
    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

    From fragment sending intentin second activity getting extrasenter image description here

    • Sharath kumar
      Sharath kumar over 6 years
      question is unclear
    • KuLdip PaTel
      KuLdip PaTel over 6 years
      getActivity() and activity.startActivity(intent) both are same?
    • Dhanu K
      Dhanu K over 6 years
      Yes, both are same
    • Michael Meyer
      Michael Meyer over 6 years
      please read the following post stackoverflow.com/questions/5265913/…
    • Umair
      Umair over 6 years
    • Dhanu K
      Dhanu K over 6 years
      plese see my updated question with screenshoots
    • OneCricketeer
      OneCricketeer over 6 years
      Put a minimal reproducible example of all the code, not screenshots or out of context snippets
    • Sangeet Suresh
      Sangeet Suresh over 6 years
      @DhanuK can you please post manifest block of LoadActivity
    • Asharali V U
      Asharali V U over 6 years
      Change intent.hasExtras("meta_data") to intent.hasExtras(""loadsPosition") in LoadActivity. Looks like you are checking a wrong key
    • SadeghAlavizadeh
      SadeghAlavizadeh over 6 years
      You can use EventBus github.com/greenrobot/EventBus for these works.
    • OneCricketeer
      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
    Dhanu K over 6 years
    please see screenshots
  • OneCricketeer
    OneCricketeer over 6 years
    new Intent(this, doesn't work from a Fragment class
  • Dhanu K
    Dhanu K over 6 years
    do you understood what question is? i am trying to send data to a activity, that is started in fragment.