How can you transfer data from a DialogFragment to the fragment that caused it and replace the display in a TextView . I have 3 tabs, each tab is a fragment, all are in the same DialogFragment in TextView for selected minutes and seconds in DialogFragment (after clicking ОК ). Using the interface, I transfer data to MainActivity , but I don’t know from there how to transfer and correctly change the TextView value.

DialogFragment code:

 //Создаем интерфейс для передачи данных в активити public interface DialogReverseListener { void onDialogPositiveClick(DialogFragment dialog,int minutes,int seconds); void onDialogNegativeClick(DialogFragment dialog); } //подписаться на действия интерфейса DialogReverseListener mListener; NumberPicker numberPickerMinutes; NumberPicker numberPickerSeconds; SharedPreferences preferences; SharedPreferences.Editor editor; int pickedMinutes = 0; int pickedSeconds = 0; TextView textMinutes, textSeconds, textSplash; public DialogReverseFragment() { // Required empty public constructor } //Переопределяем что б был доступ к Интерфейсу @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (DialogReverseListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + "must implements DialogReverseListener"); } } @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle("Выберите время"); //Через фрагмент находим переференсе preferences = getActivity().getPreferences(Context.MODE_PRIVATE); editor = preferences.edit(); LayoutInflater inflater = getActivity().getLayoutInflater(); View contentView = inflater.inflate(R.layout.fragment_dialog_reverse, null); numberPickerMinutes = (NumberPicker) contentView.findViewById(R.id.numberPickerMinutes); numberPickerSeconds = (NumberPicker) contentView.findViewById(R.id.numberPickerSeconds); numberPickerMinutes.setMaxValue(60); numberPickerMinutes.setMinValue(0); numberPickerSeconds.setMaxValue(60); numberPickerSeconds.setMinValue(0); textMinutes = (TextView) contentView.findViewById(R.id.textViewReverseMinutes); textSeconds = (TextView) contentView.findViewById(R.id.textViewReverseSeconds); textSplash = (TextView) contentView.findViewById(R.id.textViewReverseSplash); builder.setView(contentView); builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.cancel(); mListener.onDialogNegativeClick(DialogReverseFragment.this); } }); builder.setNegativeButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { int minutes = numberPickerMinutes.getValue(); int seconds = numberPickerSeconds.getValue(); mListener.onDialogPositiveClick(DialogReverseFragment.this,minutes,seconds); dialogInterface.dismiss(); } }); final AlertDialog alertDialog = builder.create(); //Смена цвета Позитив и Негатив кнопок alertDialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialogInterface) { alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary)); alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimary)); } }); return alertDialog; } 

In the MainActivity implement the interface:

 public class MainActivity extends AppCompatActivity implements DialogReverseFragment.DialogReverseListener public void onDialogPositiveClick(DialogFragment dialog,int minutes,int seconds) { @Override public void onDialogPositiveClick(DialogFragment dialog,int minutes,int seconds) { //Как отсюда передать данные обратно в фрагмент и заменить вид TextView } 

How to redraw TextView on the data that I received from the dialog (initial value 00:00 )

  • perhaps a similar answer with the DatePicker will help you. - pavlofff
  • It is also recommended to use the TimePicker widget for entering the time, rather than several NumberPicker , since it provides a more convenient interface for returning the entered time - pavlofff
  • You see, I have an analogue of the stopwatch, I don’t need time, and the value of the minute and seconds for the report is Viktor Ganich

1 answer 1

You can use the setTargetFragment method - then the result will be returned exactly to the fragment you need;) Example of calling the dialog from the fragment:

  DialogFragment dialogFragment = ShopSignFragmentDialog.newInstance(); dialogFragment.setTargetFragment(this, REQUEST_CODE_SHOP_SIGN); dialogFragment.show(getFragmentManager(), "ShopSignFragmentDialog"); 

In the dialog that we call and which is inherited from DialogFragment, we use the method to transfer the result.

 getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent); 

After that, we process the result in the calling fragment in the onActivityResult method. Example:

 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_SHOP_SIGN) { if (resultCode == Activity.RESULT_OK) { //do something } } else super.onActivityResult(requestCode, resultCode, data); } 
  • Thank you so much for your help, your advice worked perfectly, but for those who will have similar problems, here is a good article: habrahabr.ru/post/259805 - Victor Ganich