Does the listener inside onCreate works more than once?

Refresh

April 2019

Views

160 time

1

I have a listener for my date picker which I defined inside onCreate which I want to work as soon as the user changed the date picker date as follows

Edited

    public class birthDate extends Activity{


        DatePicker birthDayDatePicker,periodDatePicker;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.birthdate);

            birthDayDatePicker = (DatePicker)findViewById(R.id.DatePickerBirthDay);

              birthDayDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener () {

                @Override
                public void onDateChanged(DatePicker birthDayDatePicker, int currentYear, int currentMonth, int currentDay) {
                    // TODO Auto-generated method stub

                    periodDateCalculations();

                    }
            });

        }

public void periodDateCalculations ()
    {
        Calendar start = Calendar.getInstance();

        // get the chosen date from birth date picker
        int birthYear = birthDayDatePicker.getYear();
        int birthMonth = birthDayDatePicker.getMonth();
        int birthDay = birthDayDatePicker.getDayOfMonth();

        //set the chosen date to calendar instance
        start.set(birthYear, birthMonth, birthDay);

        //get the date 
        Date birthDate = start.getTime();
        //int constrain = currentMonth + 9;

        int daysToAdd = -280;

        //another instance from calendar 
        Calendar cal2 = Calendar.getInstance();

        //set birth date chosen to calendar
        cal2.setTime(birthDate);

//      if (cal.getTime().getMonth() >= constrain)
//      {System.out.println("cal.getTime().getMonth() >= constrain");}

        //add -280 day to birth day to get period date
        cal2.add(Calendar.DAY_OF_MONTH,daysToAdd);
        System.err.println("-----" +cal2.getTime());
        int periodYearAfterCalc = cal2.getTime().getYear()+1900;
        System.err.println("-----" + periodYearAfterCalc);
        int periodMonthAfterCalc = cal2.getTime().getMonth();
        System.err.println("----" + periodMonthAfterCalc);
        int periodDayAfterCalc = cal2.getTime().getDate();
        System.err.println(periodDayAfterCalc);

        birthDayDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
        periodDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);

        //edit birth date picker from user  
            if(birthDay >= currentDay && birthMonth >= currentMonth && birthYear >= currentYear){
                //the program runs normally
                periodDatePicker.updateDate(periodYearAfterCalc, periodMonthAfterCalc, periodDayAfterCalc);     

                }
                else{
                                new AlertDialog.Builder(birthDate.this)

                                .setTitle("Wrong Data Input!")

                                .setMessage("birth day input error")

                                .setNeutralButton("Ok",

                                new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog,

                                int which) {


                                }

                                }).show();

                          periodDatePicker.updateDate(currentYear, currentMonth, currentDay);   

                            }

        }

My question is, does this listener works more than once as its defined inside onCreate , or I should define it some where else to use it more than once ??

Thanks in advance

1 answers

1

The fact that it's defined in onCreate() is irrelevant. You're initializing a date picker with a given listener, and the date picker will thus invoke this listener each time its date changes.