user3155478
user3155478

Reputation: 1055

Callback from DialogFragment to MainActivity - Notify MainActivity when done

I have developed a simple DatePicker (through android devolper) app in android for testing. I have used Fragment Dialog. Everything works just fine and after selecting the date and then klick ok a callback is made to the overridden onDateSet(DatePicker view, int year, int month, int day)

So far so good. But this is a static inner class of the outer MainActivity-class and I cannot right now figure it out how I could instead make a callback to the mainAcitity? I have three fields in the MainActivity (year, month, day) that I want to be set after klicking "ok" in the FragmentDialog. I can only access the fields in the inner static class right now.

It may sound silly this - but how could I in a nice manner notify the MainActivity when "ok" in the fragment is click and then immediately receive YEAR, MONTH and DAY.

Thanks!!!

public class MainActivity extends FragmentActivity {

   private int year, month, day;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

   }

   public void showTimePickerDialog(View v) {
       DialogFragment newFragment = new DatePickerFragment();
       newFragment.show(getSupportFragmentManager(), "datePicker");
   }


   public static class DatePickerFragment extends DialogFragment
   implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar c = Calendar.getInstance();

        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        return new DatePickerDialog(getActivity(), DatePickerFragment.this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        System.out.println("year = " + year + "\n month = " + month + "\nday = " + day);

    }
}

xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

     <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"
       android:text="test" 
       android:onClick="showTimePickerDialog" />

</RelativeLayout>

Upvotes: 0

Views: 582

Answers (2)

Eduardo Proa&#241;o
Eduardo Proa&#241;o

Reputation: 1

Since you have the getActitity() method, you just can cast the activity to your class such as MainActivity, and the call the public method you like:

.setPositiveButton("Jugar de Nuevo", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { MainActivity activity = (MainActivity) getActivity(); activity.initGame(); } })

Upvotes: 0

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

No need for callbacks, what you need to do is to override the positive button or the OK button and call a method from the activity to notify that button is pressed which by then you " immediately receive YEAR, MONTH and DAY." from that method of your activity

sample:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Calendar c = Calendar.getInstance();

    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog date = new DatePickerDialog(getActivity(), DatePickerFragment.this, year, month, day);
    date.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    notifyActivity(); //must be the method from the activity
                }
            });
    return date;
}

Upvotes: 3

Related Questions