0

I have a pop-up dialog that asks for a user input.

Once the user entered his/her input and then click 'OK', I wish the fragment to pass the user input back to the activity that is just behind the dialog.

I have read something about onDismiss(DialogInterface dialog). However, being a new learner of both Java and Android, I fail to implement it correctly:

  1. The handler never runs in my code.

  2. Even after the handler runs, I still do not know how to get the user input from the argument dialog, since it is of type DialogInterface, which I do not know how to deal with.

Below are my code snippets.

Activity side:

    public class CalibrationActivity extends Activity implements SensorEventListener, DialogInterface.OnDismissListener {

    ...

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_calibration);

            buttonInit();
            sensorInit();
        }


        private void buttonInit() {

            // start calibrating button
            startCalibratingButton = (Button) findViewById(R.id.button1);
            startCalibratingButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {

                    startCalibrationFlag = true;

                    Toast.makeText(CalibrationActivity.this, "Please walk " + Constant.CALIBRATION_STEP_NUMBER + " steps and press the 'Done Calibrating' button", Toast.LENGTH_SHORT).show();
                    startCalibratingButton.setEnabled(false);
                    doneCalibratingButton.setEnabled(true);
                }
            });

            // done calibrating button
            doneCalibratingButton = (Button) findViewById(R.id.button2);
            doneCalibratingButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {

                    startCalibrationFlag = false;

                    RequireDistanceDialogFragment requireDistanceDialogFragment = new RequireDistanceDialogFragment();
                    requireDistanceDialogFragment.setCancelable(false);
                    requireDistanceDialogFragment.show(getFragmentManager(), ALARM_SERVICE); // dialog pops up that leads the new user to calibrate

                // Error Here: "The method setOnDismissListener(new OnDismissListener(){}) is undefined for the type RequireDistanceDialogFragment"
                    requireDistanceDialogFragment.setOnDismissListener(new OnDismissListener() {

                        public void onDismiss(DialogInterface dialog) {
                            LoadingActivity.this.finish();
                        }
                    });

                }
            });

            startCalibratingButton.setEnabled(true);
            doneCalibratingButton.setEnabled(false);
        }

        // called when the dialog is dismissed
        public void onDismiss(DialogInterface requireDistanceDialogFragment) {

// Error Here: The method getDistanceEntered() is undefined for the type DialogInterface
                double userInput = requireDistanceDialogFragment.getDistanceEntered();
                processTheUserInputHere();

        }

    }

Dialog side:

package com.example.drsystem;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.widget.EditText;
import android.widget.Toast;

public class RequireDistanceDialogFragment extends DialogFragment {

    private Editable distance;
    private boolean distanceIsEntered = false;

    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        final EditText input = new EditText(getActivity());
        input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

        builder.setTitle("Distance is required to calibrate the stride length estimation.").setMessage("Distance (in m) for these " + Constant.CALIBRATION_STEP_NUMBER + " steps?").setView(input).setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) { // click ok

                distanceIsEntered = true;
                distance = input.getText();
            }
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) { // click cancel

                Toast.makeText(getActivity(), "Calibration not done", Toast.LENGTH_SHORT).show();
            }
        });

        // Create the AlertDialog object and return it
        return builder.create();
    }

    // if the user has not entered the distance yet, the distance returned is -1
    public double getDistanceEntered() {

        if (distanceIsEntered)
            return Double.parseDouble(distance.toString());
        else
            return -1;
    }

}

Either you can help me modify it by solving the 2 problems I have

or you can propose new method to achieve this.

Sibbs Gambling
  • 19,274
  • 42
  • 103
  • 174
  • Use startActivityForResult(calibrationIntent, 0); and check if it makes the difference this link might help you in achieving the above idea http://stackoverflow.com/questions/10407159/android-how-to-manage-start-activity-for-result – ingsaurabh Jun 21 '13 at 06:20
  • @ingsaurabh it helps. But the problem is that the pop-up dialog is NOT an activity. So when it is finished, the `onActivityResult()` won't be called, will it? – Sibbs Gambling Jun 21 '13 at 06:45
  • ok earlier in code its written startActivity so I have suggested that now try this http://stackoverflow.com/questions/12622742/get-value-from-dialogfragment – ingsaurabh Jun 21 '13 at 07:00

1 Answers1

1

your onClickMethod in the dialog should put the input in a message

builder.setMessage("message from user");

Your onDismiss listener should get the data from the dialog before it is killed

public void onDismiss(DialogInterface dialog) { String messageFromUser = dialog.getMessage("what ever string you want"); LoadingActivity.this.finish(); }

Why are you finishing the loading activity?

Rubber Duck
  • 3,673
  • 3
  • 40
  • 59
  • I am so so sorry! I paste the wrong dialog code snippet. Please check again. Actually my dialog does not start a new activity. Please modify ur answer accordingly. :) – Sibbs Gambling Jun 21 '13 at 06:09