I have a custom DialogFragment and the question is this ... There is an activit which, using the newInstance(int layout) method, gets an instance of the new MyDialog() class in which there are 2 buttons on which the licenser hangs and listens to clicks. Now when you click on the DialogFragment button, DialogFragment just closes ... So I can not guess how I can make it return to the values ​​that caused it before closing (let's say a string string).

Here is the code:

 public class MyDialog extends DialogFragment { final String LOG_TAG = "MyDialog"; private static MyDialog myDialog; int layout; @Override public void onCreate(Bundle savedState) { super.onCreate(savedState); Bundle args = this.getArguments(); layout = args.getInt("layout"); } public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder adb = new AlertDialog.Builder(getActivity()) .setView(layout) .setCancelable(true); if (layout == R.layout.activity_note_dialog) { LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(layout, null); LinearLayout llSent = (LinearLayout) view.findViewById(R.id.llSent); LinearLayout llSkip = (LinearLayout) view.findViewById(R.id.llSkip); llSent.setOnClickListener(listener); llSkip.setOnClickListener(listener); adb.setView(view); } return adb.create(); } View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.llSent: System.out.println("FFFFFFFFFFFFFFFFFFFFFFFFFFF"); myDialog.dismiss(); break; case R.id.llSkip: System.out.println("DDDDDDDDDDDDDDDDDDDDDDDDDD"); myDialog.dismiss(); break; } } }; public void dismissDialog(final MyDialog dialog, int daleyTime) { Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { try { dialog.dismiss(); } catch (IllegalStateException e) { e.printStackTrace(); } } }, daleyTime); } public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); Log.d(LOG_TAG, "MyDialog: onDismiss"); } public void onCancel(DialogInterface dialog) { super.onCancel(dialog); Log.d(LOG_TAG, "MyDialog: onCancel"); } public static MyDialog newInstance(int layout) { myDialog = new MyDialog(); Bundle args = new Bundle(); args.putInt("layout", layout); myDialog.setArguments(args); return myDialog; } } 

Tell me how to do this?

3 answers 3

You need to add your own interface, make a setter for it in the Dialogue, set up the interface in the redefined method, write everything you need, and in the dialog box, call this method by pressing a button.

    You can, for example, write a method in the activation, with a argument with arguments and call it from the dialogue by casting the result of calling getActivity() to a specific class of your activity:

    In the dialogue class:

     ((ActivityClassName)getActivity()).someMethodName(someArg); 

    In the activit class:

     public void someMethodName(SomeArgType someArg){ Log.d("TAG", someArg.toString()); } 
    • Not a very good option - artemiygreg
    • @artemiygreg, well, as an option, it can still help. Although yes - with the interface - better) - YuriySPb
    • as an option, yes, it can be done to work, but the approach itself is bad, let’s then we’ll need to do the same thing in two activations and how to implement it, checks via instanceof?)) - artemiygreg
    • @artemiygreg, just make an abstract basic activit, in it this method of defining and casting to it - no instanceof will be needed))) - JuriySPb
    • 2
      @artemiygreg, well, so I’m saying that the interface is better. And not only speak, but also put a plus. But, as I remember, the interfaces are difficult and better for beginners, as I suggested at first to do - quickly and clearly. Although not quite right, yes. - Yuriy SPb

    As a result, made through the interface, thanks @Abrog Petrovich the answer was here ,

    The bottom line is:

    In the DialogFragment class:

      public interface OnButtonClick { void onDialogClickListener(int action, int type); } OnButtonClick onClick; @Override public void onAttach(Activity activity) { super.onAttach(activity); try { onClick = (OnButtonClick) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement onDialogClickListener"); } } @Override public void onDetach() { super.onDetach(); onClick = null; } 

    In MainActivity :

     ...extends AppCompatActivity implements MyDialog.OnButtonClick {... @Override public void onDialogClickListener(int action, int type) {} 

    Usage: in DialogFragment at the right time -

      onClick.onDialogClickListener(action, type); 

    You can transfer absolutely any data.