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?