title: Android's AlertDialog in fragment
body: <p>I have an app that uses tabbed activity, with 3 tabs, each with a separate fragment and layout. Tab3 has some user-profile settings, like a name for example. Name is displayed inside a cardview, so I made that cardview clickable, and onClick it would open an alertDialog with an edit text to enter the name. to build this dialog I found 2 methods.</p>
  <p><strong>First:-</strong> (this is run once the cardview is clicked(On clicklistener)) and its working very well and I have no issues with it but it just doesn't feel like a best-practice</p>
  <pre><code>AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
  
                  alert.setTitle(&quot;Enter your Username&quot;);
  
                  LayoutInflater inflater = getActivity().getLayoutInflater();
                  View view1 = inflater.inflate(R.layout.editname_layout, null);
                  alert.setView(view1);
                  final EditText input = view1.findViewById(R.id.editname);
                  alert.setPositiveButton(&quot;Save&quot;, new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int whichButton) {
  
                          textView.setText(input.getText().toString());
                      }
                  });
  
                  alert.setNegativeButton(&quot;Cancel&quot;, new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int whichButton) {
                          // Canceled.
                      }
                  });
  
                  alert.show();
  </code></pre>
  <p><strong>Second:-</strong> is that I build a separate DialogFragment with an interface to send data back to fragment this one feels like a best practice but i have faced many issues with it, such as.. when data is sent back to my fragment, I can't use the method &quot;textView.setText&quot; on the received data as I receive it outside the onCreateView method and thus textView always returns null.</p>
  <pre><code>public class Dialog extends AppCompatDialogFragment {
      
      private Context context;
      public DialogListener listener;
  
      public Dialog(Context context) {
          this.context = context;
      }
  
      @NonNull
      @Override
      public android.app.Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
          AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
          
              LayoutInflater inflater = getActivity().getLayoutInflater();
              View view = inflater.inflate(R.layout.name_layout, null);
              final EditText name = (EditText) view.findViewById(R.id.editname);
              builder.setView(view)
                      .setTitle(&quot;Username&quot;)
                      .setNegativeButton(&quot;Cancel&quot;, new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialogInterface, int i) {
  
                          }
                      })
                      .setPositiveButton(&quot;Save&quot;, new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialogInterface, int i) {
                              String text = name.getText().toString();
                              listener.data(text);
                              Toast.makeText(getActivity(), &quot;Saved&quot;, Toast.LENGTH_SHORT).show();
                          }
                      });
          
        
           return builder.create();
      }
  
      @Override
      public void onAttach(@NonNull Context context) {
          super.onAttach(context);
  
          try {
              Fragment fragment = new Tab3();
              listener = (DialogListener) fragment;
          }
          catch (ClassCastException e) {
              throw new ClassCastException(context.toString() + &quot; must implement d.l&quot;);
          }
      }
  
  
  
      public interface DialogListener {
          void data(String name);
      }
  }
  </code></pre>
  <p>So what I'm thinking is that I should go with the first method as it's easier, and it's easier to extract text from it but still not sure if it would be a good practice.</p>
  <p>thanks in advance!</p>