Joan Z
Joan Z

Reputation: 43

How to pass value from one fragment to another

I'm new to android and I have a hard time understanding how to pass value from fragment to another fragment. I have read other similar thread but I just can't seem to get it right. I have a datepicker fragment that I need to send some data to another fragment (itemfragment). I have this in datepicker fragment

@Override
public void onDateSet(DatePicker view, int year, int month, int date) {

    calendar.set(year, month + 1, date);

    int selDay = view.getDayOfMonth();
    int selMon = view.getMonth();
    int selYear = view.getYear();

    //set calendar to selected date
    Calendar c = Calendar.getInstance();
    c.set(selDay, selMon, selYear);
    c.add(Calendar.DATE, -1);
    Long twentyfourhrs = c.getTimeInMillis();
}

public interface sendValue{
    void sendData(Long twentyfourhrs);
}

sendValue sv;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        sv = (sendValue) activity;
    }
    catch (ClassCastException e){
        throw new ClassCastException("implement sendValue method");
    }
}

Then in MainActivity

public class MainActivity extends AppCompatActivity implements DatePickerFragment.sendValue{

@Override
public void sendData(Long twentyfourhrs) {
    Bundle bundle = new Bundle();
    bundle.putLong("24", twentyfourhrs);
    ItemFragment if = new ItemFragment();
    if.setArguments(bundle);
}

in ItemFragment

Long reminder24;

public void setArguments(Bundle bundle){
    super.setArguments(bundle);
    reminder24 = bundle.getLong("24", twentyfourhrs);
}

AdapterView.OnItemSelectedListener listener = new AdapterView.OnItemSelectedListener() {

    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

if...{
...}
else{
if (reminder.equals("24 hours")) {
makeToast(reminder + " from " + selected_date + " is "  + reminder24);

I'm not sure what I did is correct or not but the reminder24 in toast came back as null. what did I do wrong?

Upvotes: 0

Views: 796

Answers (2)

Pavan Bilagi
Pavan Bilagi

Reputation: 1617

From Sending Fragment end use Bundle to send data from one fragment to another

Fragment fragment = new Fragment();
            Bundle args = new Bundle();
            args.putString("value", key);
            fragment.setArguments(args);

At the receiving Fragment to get data use this inside onCreateview() or onCreate() or onActivityCreated()

Bundle myData = this.getArguments();
String data = myData.getDouble("value","data");

Upvotes: 1

marc97
marc97

Reputation: 128

I don't know how are you passing the arguments to the fragment but these have to be received like this:

Bundle bundle = getArguments();
bundle.getLong(/*default value*/, /*key*/); 

This is normally obtained inside onCreateView(); method.

Note: For a well practice you should declare variables with these modifiers public static final containing the key for the arguments you need to send.

Upvotes: 0

Related Questions