Search code examples
androidandroid-intentbroadcastreceivercustomdialog

Pass information from CustomDialog to main Activity


I'm writting an app which manages alarms and events as in a calendar app. I ran into a problem when I tried to get all the info about the event that was reported in a custom dialog. I tried many things. The last one thing to that I saw on internet uses intents. However latheough the Dialog can send the info, I dont know how to receive it. I try something with broadcast recevier but it didn't work...

-->Here is the code for the dialog:

public class AddingEventMenu extends Dialog implements OnClickListener {

Event event = new Event();
private Button b;
Context context;

public AddingEventMenu(Context context) {
    super(context);
    this.context = context;
    this.setContentView(R.layout.addbuttonlayout);
    this.setTitle("Adding Event");
    this.setCancelable(true);

    this.b = (Button) findViewById(R.id.addEventButton);
    this.b.setOnClickListener(this);

}

public void onClick(View v) {
    String color;
    DatePicker DP = (DatePicker) findViewById(R.id.datePicker);
    TimePicker TP = (TimePicker) findViewById(R.id.timePicker);
    EditText ET = (EditText) findViewById(R.id.eventText);
    Spinner SC = (Spinner) findViewById(R.id.colorSelector);
    Intent intent = new Intent();
    intent.setAction("New Event");


    color = SC.getSelectedItem().toString();

    if (color == "Red") event.eventColor = Color.RED;
    if (color == "Green") event.eventColor = Color.GREEN;
    if (color == "Blue") event.eventColor = Color.BLUE;
    if (color == "Magenta") event.eventColor = Color.MAGENTA;

    intent.putExtra("Text", ET.getText().toString());
    intent.putExtra("Day", DP.getDayOfMonth());
    intent.putExtra("Month", DP.getMonth());
    intent.putExtra("Year",  DP.getYear());
    intent.putExtra("Hour",  TP.getCurrentHour() * 100 + TP.getCurrentMinute());
    intent.putExtra("Color", color);

    context.sendBroadcast(intent);

    Log.d("State", "Es un primer paso");

    this.dismiss();


}

-->Here the main menu code:

public class Timetable extends Activity {
public static List<Event> eventList;
EventManager eventManager;
LinearLayout eventListLayout;
ImageView addButton;
EventDatabaseManager db;
BroadcastReceiver BR;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.layout_timetable);

    BR = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction() == "New event"){
                Bundle extras = new Bundle();
                Event event = new Event();
                extras = intent.getExtras();
                event.eventText = extras.getString("Text");
                event.eventDay = extras.getInt("Day");
                event.eventMonth = extras.getInt("Month");
                event.eventYear = extras.getInt("Year");
                event.eventHour = extras.getInt("Hour");
                event.eventColor = extras.getInt("Color");

                Log.d("State", "Esta funcionando");

                saveNewEvent(event);
            }
        }
    };

    db = new EventDatabaseManager(this);

    eventListLayout = (LinearLayout) findViewById(R.id.eventListLayout);

    eventManager = new EventManager(this);
    eventList = eventManager.getEvents();



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.layout_timetable, menu);
    return true;
}

public void onAddButton(View button) {
    AddingEventMenu aEM = new AddingEventMenu(this);

    aEM.show();

}

public void saveNewEvent(Event event) {
    eventList.add(event);
    db.addEvent(event);

}

In the main Activity I defined the broadcastreceiver, but I think that it's wrong, because it doesn't anything there.

I hope someone can help me. Thanks for your time.


Solution

  • In order to be future compatible you should consider using DialogFragment instead of Dialog.

    When using the DialogFragment you can add a custom listener to the DialogFragment class like this one:

    public interface EventAddedListener {
      public void onEventAdded(Event event);
    }
    

    and then add it to your DialogFragment.

    After an event has been chosen you call listener.onEventAdded(event); and handle the result within your Activity.

    But you can also work with the more complicated IntentFilter solution provided by sinisha, even when using a DialogFragment.