0
I have fragment1 and Datepickerfragment
In the case DatePickerFragment extends DialogFragment
I would like when selecting the date to return to Fragment.
Only you’re giving it here:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Fragment.onActivityResult(int, int, android.content.Intent)' on a null object reference
The method of recovery is this:
In the DatePickerFragment extends DialogFragment
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
//...
SimpleDateFormat data_br = new SimpleDateFormat( "dd/MM/yyyy" );
String data = data_br.format( date );
Intent i = new Intent();
i.putExtra( "selectedDate",data );
getTargetFragment().onActivityResult( getTargetRequestCode(), 2, i );
}
And on the onActivityResult:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("RequestCode",""+requestCode);
if( requestCode == 1 ){
}
}
How do I return the selected value in Dialogfragment to the fragment ?
Are you sure this works?
– ramaral
@ramaral here with me works in an Activity. I did not test with Fragment. In my head follows same logic. I will test later and confirm here.
– viana
I asked because I don’t see how Datepickerdialog knows that this method has been implemented in Articlefragment.
– ramaral
I tested and tested and managed to call the Datepickerfragment inside the Ragment itself, ai in onDateSet and just set the value I wanted, ai it worked
– adventistaam
@adventistaam as Ramaral questioned, I was testing here on a Fragment. Because in fact I had only done one Activity. I was already editing the question
– viana
Great then! I’m starting in this area of communication between Fragments
– adventistaam
How the AP says it worked was to look how the Datepickerdialog would have known the Listener. He is informed in the builder:
DatePickerDialog(Context context, DatePickerDialog.OnDateSetListener listener, int year, int month, int dayOfMonth)
.– ramaral