简体   繁体   中英

Handling the buttons from dialogfragment in main activity

I want to handle the newgame button and add players button in my main activity. As you can see I'm defining the view in my warningdialog class (which extends DialogFragment, I tried having a getter and tried calling that but I get nullpointer exception, I tried other methods as well but the app crashed.

Whats the best way of doing this? The reason y I want to do this, is because I have other methods in main activity the I want to call when the button is pressed, so I think handling the dialogbutton from main activity is the best choice.

Here is my Warningdialog which extends from dialogfragment

public class WarningDialog extends DialogFragment {

public Button mNewGame, mAddPlayersBtn;
private ImageButton mCloseBtn;


/**
 * Method to close the dialog
 */
private View.OnClickListener mBtnListener = new View.OnClickListener(){
    @Override
        public void onClick(View v){
            dismiss();
    }
};

public WarningDialog() {

}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogTheme);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.newgame_dialog,container,false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mAddPlayersBtn = (Button) view.findViewById(R.id.btnAddPlayers);
    mNewGame = (Button) view.findViewById(R.id.btnNewGame);
    mCloseBtn = (ImageButton) view.findViewById(R.id.btnClose);

    //closing the dialog
    mCloseBtn.setOnClickListener(mBtnListener);

}


public Button getmNewGame() {
    return mNewGame;
}

Main activity method:

    private void warningDialog(){
    final WarningDialog warningDialog = new WarningDialog();
    warningDialog.show(getSupportFragmentManager(),"Warning");

    warningDialog.getmNewGame().setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
        }
    });

}

All you need to do is have the DialogFragment call back into a public method in the Activity.

In the DialogFragment, add a new OnClickListener for the new game button:

/**
 * Method to start new game
 */
private View.OnClickListener mNewGameBtnListener = new View.OnClickListener(){
    @Override
        public void onClick(View v){
            ((MainActivity)getActivity()).doNewGame();
            dismiss();
    }
};

Then set this as the click listener for the new game button:

 mNewGame = (Button) view.findViewById(R.id.btnNewGame);
 mNewGame.setOnClickListener(mNewGameBtnListener);

Then, in the Activity, just define the public method that will be called on click of the DialogFragment button:

public void doNewGame() {
    //User chose new game!
}

You can call Activity.onActivityResult from the DialogFragment, and then based on the request & response codes respond appropriately. I don't think your approach can work, because the buttons need to be initialized in onCreateView etc, which your activity won't know about.

public class WarningDialog extends DialogFragment {
    public static int RC_WARNING = 100;
    public static int RC_NEW_GAME = 101;
    public static int RC_ADD_PLAYER = 102;

    /**
     * Method to handle clicks in the dialog
     */
    private View.OnClickListener mBtnListener = new View.OnClickListener(){
        @Override
        public void onClick(View v){
            if (v == mCloseBtn) {
              dismiss();
            } else if (v == mNewGame) {
              getActivity().onActivityResult(RC_WARNING, RC_NEW_GAME, null);
            } else if (v == mAddPlayersBtn) {
              getActivity().onActivityResult(RC_WARNING, RC_ADD_PLAYER, null);
            }
        }
    };
}

// In your activity:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (request_code == WarningDialog.RC_WARNING) {
        if (resultCode == WarningDialog.RC_NEW_GAME) {
          // new game logic
        } else if (resultCode == WarningDialog.RC_ADD_PLAYER) {
          // add player logic
        }        
    }
}

You can also pass additional data using the Intent parameter on onActivityResult .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM