0

i am trying to make a app which set a alarm according to the timepicker, i have done this but i want the edittext from the mainactivity to be put into the alert dialog which is presented when the alrm goes off.

i want to send text from mainactivity to alertdemo.

MainActivity.class

private void stalarm() {
    View.OnClickListener setClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            /** This intent invokes the activity DemoActivity, which in turn opens the AlertDialog window */
            Intent i = new Intent("app.com.timepicker.demoactivity");

            /** Creating a Pending Intent */
            PendingIntent operation = PendingIntent.getActivity(getBaseContext(), unique, i, PendingIntent.FLAG_CANCEL_CURRENT);

            /** Getting a reference to the System Service ALARM_SERVICE */
            AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);

            /** Getting a reference to TimePicker object available in the MainActivity */
            TimePicker tpTime = (TimePicker) findViewById(R.id.tp_time);

            Calendar c = Calendar.getInstance();

            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);
            int hour = tpTime.getCurrentHour();
            int minute = tpTime.getCurrentMinute();

            /** Creating a calendar object corresponding to the date and time set by the user */
            GregorianCalendar calendar = new GregorianCalendar(year, month, day, hour, minute);

            /** Converting the date and time in to milliseconds elapsed since epoch */
            long alarm_time = calendar.getTimeInMillis();

            /** Setting an alarm, which invokes the operation at alart_time */
            alarmManager.set(AlarmManager.RTC_WAKEUP, alarm_time, operation);

            /** Alert is set successfully */
            Toast.makeText(getBaseContext(), "Alarm is set successfully", Toast.LENGTH_SHORT).show();
        }
    };

    Button btnSetAlarm = (Button) findViewById(R.id.btn_set_alarm);
    btnSetAlarm.setOnClickListener(setClickListener);

}

demoactivity.class

public class DemoActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /** Creating an Alert Dialog Window */
    AlertDemo alert = new AlertDemo();

    /** Opening the Alert Dialog Window. This will be opened when the alarm goes off */
    alert.show(getSupportFragmentManager(), "AlertDemo");

}

alertdemo.class

public class AlertDemo extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    /** Turn Screen On and Unlock the keypad when this alert dialog is displayed */
    getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

    /** Creating a alert dialog builder */
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    /** Setting title for the alert dialog */
    builder.setTitle("alarm " );

    /** Setting the content for the alert dialog */
    builder.setMessage("An Alarm by AlarmManager");

    /** Defining an OK button event listener */
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            /** Exit application on click OK */
            getActivity().finish();
        }
    });

    /** Creating the alert dialog window */
    return builder.create();
}

/** The application should be exit, if the user presses the back button */
@Override
public void onDestroy() {
    super.onDestroy();
    getActivity().finish();
}

}
user3694517
  • 11
  • 1
  • 3

1 Answers1

1

If what you want to do is send the contents of your EditText to the DialogFragment to do something with that information, you can use the setArguments() functionality of Fragments. This question covers a good pattern for doing this.

Ultimately, you'll want to create a newInstance for your DialogFragment with a String argument. That argument is put in the fragment's arguments bundle in newInstance, and accessed when needed later in the fragment's life cycle.

When you call your newInstance simply do DialogFragment.newInstance(editText.getText() to get the DialogFragment you want to pass to your fragment manager.

Community
  • 1
  • 1
emerssso
  • 2,376
  • 18
  • 24