0

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?

Akshay Chordiya
  • 4,761
  • 3
  • 40
  • 52
Joan Z
  • 43
  • 1
  • 9
  • Possible duplicate of [Best practice for instantiating a new Android Fragment](http://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment) – OneCricketeer Dec 06 '15 at 15:18
  • I found a solution that works for me http://stackoverflow.com/a/34993646/5563156 – Joan Z Jan 28 '16 at 15:31
  • Okay. I fail to see how that answer is different than those here or the post I linked to – OneCricketeer Jan 28 '16 at 17:33

2 Answers2

1

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");
Rajesh Satvara
  • 3,842
  • 2
  • 30
  • 50
Pavan Bilagi
  • 1,618
  • 1
  • 18
  • 23
  • but the value hasn't been entered during onCreate of the receiving fragment (ItemFragment). ItemFragment is where the user enter input. In ItemFragment, there's a button when user click will pop up datepicker dialog (hence the datepicker fragment). – Joan Z Dec 07 '15 at 09:34
  • use it in onCreateView() , 1st check whether your sending data or not , put some toast to check value for sending and receiving ends – Pavan Bilagi Dec 07 '15 at 12:10
0

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.

marc97
  • 128
  • 2
  • 7