Pass value from Dialogfragment to Fragment

Asked

Viewed 268 times

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 ?

1 answer

1

Implement in your Fragment the DatePickerDialog.OnDateSetListener. See an example below:

public class ArticleFragment extends Fragment implements DatePickerDialog.OnDateSetListener {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        return inflater.inflate(R.layout.article_view, container, false);
    }
}

It will therefore be necessary to include the onDateSet without having to use the onActivityResult. The end result would be:

public class ArticleFragment extends Fragment implements DatePickerDialog.OnDateSetListener {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        return inflater.inflate(R.layout.article_view, container, false);
    }

    @Override
    public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
        // aqui você resgata o ano, mês, dia etc...
    }
}
  • Are you sure this works?

  • @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.

  • I asked because I don’t see how Datepickerdialog knows that this method has been implemented in Articlefragment.

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

  • Great then! I’m starting in this area of communication between Fragments

  • 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).

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.