I have a class that inherits from FragmentDialog and communicates with the activation through the interface.

But the problem is that when you create a dialogue class object, you need to remember to implement the interface, and then when the dialogue tries to call a method on the interface, the application will break ...

It is clear that it is possible when I wrote everything to test and see that everything worked ... But I think you can somehow make it mandatory?

  • Who should implement the interface? Activity? - Vladyslav Matviienko
  • one
    do you write without starting? and why? - Yura Ivanov
  • @metalurgus yes activit - Aleksey Timoshchenko
  • @YuraIvanov where am I writing without starting? - Aleksey Timoshchenko
  • See the first option in the answer @ P.llyin. I would do that. If you need to explain more, ask him or me. - Vladyslav Matviienko

2 answers 2

  1. If the interfaces are from the same method, and even from the same argument, and even the streams in the project, then you can replace them with consamers and declare them in the class with an empty body right away so that the application does not fall.

     public class A { private Consumer<String> mSomeStringListener = val -> {}; } 
  2. Capture the class constructors and allow instances to be created only by a special builder who will force the setters for the interfaces to the user

  3. Create a constructor with all the necessary interferses, so as not to forget anything.

  • Consumer on android? Has it already been spoiled for Java 8? - Nofate
  • No, but we have at work on all projects of retrolamd and stream, so I'm so used to these buns that I forget to add something like "if there is a stream, then ..." - P. Ilyin
  • @KirillStoianov, Android N Preview SDK. - Nofate
  • So I still do not understand about whether it is already on the android? - Aleksey Timoshchenko

Can pass an item to newInstance ?

 public class NewDialogFragment extends DialogFragment { private Interface privateInterface; public static NewDialogFragment newInstance(Interface i){ NewDialogFragment frag = new NewDialogFragment(); frag.privateInterface = i; return frag; } public NewDialogFragment(){ } } 
  • FragmentDialog requires using the default constructor, and all that needs to be passed to Bandle - Aleksey Timoshchenko
  • I agree, the wrong decision. I will change the answer. - Alex Vorobiev
  • Yes, but how then to close access to default designers? - Aleksey Timoshchenko
  • It is not recommended to make a private constructor ... You can try so that the Interface implements Serializable and Bandle it to Bandle . - Alex Vorobiev
  • You can privateInterface make the @NonNull property privateInterface but it seems to me that this will not solve the problem. - Alex Vorobiev