title: Calling a method in a Fragment from an AlertDialog
body: <p>Could you please help with the below:</p>
  
  <p>I am trying to call the method <code>deletePlayer</code> inside the fragment <code>PlayersActivityFragment</code> from the alertdialog <code>NameAlertDialogFragment</code>.</p>
  
  <p>The code is below:</p>
  
  <pre><code>    public static class PlayersActivityFragment extends Fragment {
  
      ArrayList&lt;Player&gt; arrayPlayers;
      ListView listViewPlayers;
      //PlayerAdapter adapter;
  
      public PlayersActivityFragment() {
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          arrayPlayers = new ArrayList&lt;Player&gt;();
  
          View rootView = inflater.inflate(R.layout.fragment_activity_players, container, false);
  
          Button buttonAddPlayer = (Button) rootView.findViewById(R.id.button_addplayers);
          buttonAddPlayer.setOnClickListener(new View.OnClickListener(){
              public void onClick(View view) {
  
                  arrayPlayers.add(new Player("Player", 0));
                  Player selectedPlayer = arrayPlayers.get(arrayPlayers.size()-1);
                  ((PlayersActivity)getActivity()).showNameDialogFragment(selectedPlayer);
  
              }
          });
  
          listViewPlayers = (ListView) rootView.findViewById(R.id.listView_playername);
  
          return rootView;
  
      }
  
      public void deletePlayer(){
          arrayPlayers.remove(arrayPlayers.size()-1);
      }
  
  }
  
  
  void showNameDialogFragment(Player player) {
      mDialog = NameAlertDialogFragment.newInstance(player);
      mDialog.show(getFragmentManager(),"SCORE DIALOG");
  
  }
  
  // Class that creates the AlertDialog
  public static class NameAlertDialogFragment extends DialogFragment {
  
      static Player selectedPlayer;
  
      public static NameAlertDialogFragment newInstance(Player player) {
          selectedPlayer = player;
          return new NameAlertDialogFragment();
      }
  
      // Build AlertDialog using AlertDialog.Builder
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
          // Get the layout inflater
          LayoutInflater inflater = getActivity().getLayoutInflater();
  
          final View view = inflater.inflate(R.layout.alertdialog_name, null);
  
          final EditText editTextName = (EditText) view.findViewById(R.id.edittext_name);
  
          return new AlertDialog.Builder(getActivity())
                  // Inflate and set the layout for the dialog
                  // Pass null as the parent view because its going in the dialog layout
                  .setView(view)
  
                  .setMessage("Enter Player's Name:")
  
                  //Set up Yes Button
                  .setPositiveButton("Done", new DialogInterface.OnClickListener() {
                      public void onClick(final DialogInterface dialog, int id) {
                          mName = editTextName.getText().toString().trim();
                          selectedPlayer.setName(mName);
                      }
                  })
  
                  .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int id) {
                          //PlayersActivityFragment playersActivityFragment = (PlayersActivityFragment) getFragmentManager().findFragmentById(R.id.container);
                          //playersActivityFragment.deletePlayer();
                          //((PlayersActivityFragment)getTargetFragment()).deletePlayer();
                          NameAlertDialogFragment.this.getDialog().cancel();
                      }
                  })
  
                  .create();
      }
  }
  </code></pre>
  
  <p>The two different ways I have tried to call the methods are commented out in the <code>.setNegativeButton</code> onClickListener:</p>
  
  <pre><code>PlayersActivityFragment playersActivityFragment = (PlayersActivityFragment) getFragmentManager().findFragmentById(R.id.container);
  playersActivityFragment.deletePlayer();
  </code></pre>
  
  <p>and </p>
  
  <pre><code>((PlayersActivityFragment)getTargetFragment()).deletePlayer();
  </code></pre>
  
  <p>Thank you!</p>