Passing value from a DialogFragment to the Fragment

0

I have fragment1 and DatePickerFragment

In case DatePickerFragment extends DialogFragment

I would like when selecting the date to return to the fragment.

But 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 recovery method is this:

No 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 no 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 the DialogFragment for the snippet?

    
asked by anonymous 08.08.2017 / 20:13

1 answer

1

Implement your Fragment o DatePickerDialog.OnDateSetListener . Here's 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);
    }
}

So you will need to include the onDateSet method without having to use 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...
    }
}
    
08.08.2017 / 23:47