1

I am building a very basic vocabulary application. The feature I am trying to implement right now is a go to feature, that is taking the user to a specific vocab term. i am doing this by prompting the user with a dialog fragment that asks the user for a page number. (dialog fragment will get triggered via a callback, button press) This is my code for doing so

public class GoToDialog extends DialogFragment{
submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String pgn = pageNumber.getText().toString();
            if(!isNumeric(pgn) || pgn.isEmpty()) {
                Toast.makeText(getActivity(), "Please enter a valid number", Toast.LENGTH_SHORT).show();
            } else {
                int pagina = Integer.parseInt(pgn);
                if(pagina <= 0 ||  pagina > total) {
                    Toast.makeText(getActivity(), String.format("Please enter a valid " +
                       "term number between 0 and %d", total), Toast.LENGTH_SHORT).show();
                } else {
                    getDialog().dismiss();
                    getFragmentManager().executePendingTransactions();
                    communicator.onDialogMessage(pagina);
                }
            }
        }
    });

Here are screenshots when I run my application

enter image description here

Screenshot2(right after screenshot 1)

enter image description here

In terms of functionality The dialog loads up fine and is able to take the user to the right location. However in that example of taking the user from term 7 to term 5, the user is taken to the right term but the dialog doesn't close as it should from getDialog().dismiss(). I know dismiss is being called because I walked through the code and communicator.onDialogMessage(pagina) returns the right term number to the activity. The dialog does close when I select another term number to go to. Does anyone see the issue? This doesn't make sense to me at all.

To close a dialog, dismiss is the correct method to use - How to correctly dismiss a DialogFragment? I also tried what a user suggested in Correct way to remove a DialogFragment: dismiss() or transaction.remove()?, which is to call executePendingTransactions().

Community
  • 1
  • 1
committedandroider
  • 8,711
  • 14
  • 71
  • 126

1 Answers1

1

If anyone's having a similar issue, the issue with my application was my OnTouchListener. When I set up on OnTouchListener to trigger the DialogFragment, here was my original code for doing so goTo - TextView

private void setUpGoToTouchListener() {
    goTo.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
             FragmentManager fm = MainActivity.this.getFragmentManager();
             GoToDialog dialog = new GoToDialog();
             Bundle bundle = new Bundle();
             bundle.putInt("Size", defMan.getTotalCount());
             dialog.setArguments(bundle);
             dialog.show(fm, "Manager");
             return true;
        }
    });
}

When the gesture(a touch) on the TextView occurs, two MotionEvents will be generated, the press, ACTION_DOWN - first finger has touched the screen, and the release, ACTION_UP - the last of the fingers has stopped touching the screen. Because two motion events occurred, two dialog fragments were created. Thats why dismiss had to be called twice in my situation to get rid of both dialog fragments. I fixed this by having a conditional test for event.getAction()

committedandroider
  • 8,711
  • 14
  • 71
  • 126