Search code examples
javaandroidandroid-edittextgetter-setterandroid-calendar

String cannot be return to previous activity and insert to its editText


I met a interest issue about getting the String from another activity to main activity.

The procedures: 1. User click the editText to trigger the calendar shows with DatePickerDialog

  1. Once the date that user picked is valid, it return to be a String and set on editText, then click submit button will send to activity_confirm.

  2. When users clicked "Edit" button, the activity_confirm will return all values and go through with dataHolder to send those data to Main activity's fragment and setText on those editText.

I can get all the values correctly except DOB, I wonder why it will return null on activity_confirm while setText() on the TextView of it, is the error existed on fragment's method? But I was confused.

Thanks for any assistance or suggestion.

Main activity:

 btn_Click.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            ......
            //Get from fragment and intent to activity_confirm
            extras.putString("confirmDOB", strDOB);
            ......
        }
 }

Fragment:

......
TextInputLayout DOBpicker;
EditText DOB;
Calendar myCalendar = Calendar.getInstance();
......

    final DatePickerDialog.OnDateSetListener date = (new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar checkValid = new GregorianCalendar(year, monthOfYear, dayOfMonth);
                Calendar minAdultAge = new GregorianCalendar();
                minAdultAge.add(Calendar.YEAR, -18);
                if (minAdultAge.before(checkValid)) {
                    //Snackbar.make(correspondence, "Applicant's Date of birth cannot be 18 or below.", Snackbar.LENGTH_LONG).show();
                    DOBpicker.setErrorEnabled(true);
                    DOBpicker.setError("Applicant's Date of birth cannot be 18 or below.");
                    strDOB = "";
                    DOB.setText(strDOB);
                    insureApplicant2.put(1, strDOB);
                    DOBpicker.clearFocus();
                } else {
                    DOBpicker.setErrorEnabled(false);
                    myCalendar.set(Calendar.YEAR, year);
                    myCalendar.set(Calendar.MONTH, monthOfYear);
                    myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    updateLabel();
                }
            }
        });

        DOB.setOnFocusChangeListener(new View.OnFocusChangeListener(){
            @Override
            public void onFocusChange(View view, boolean isFocus){
                if(isFocus){
                    new DatePickerDialog(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, date, myCalendar
                            .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                            myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                }
            }
        });
    private void updateLabel(){
        String myFormat = "dd/MM/yyyy"; //In which you need put here
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
        DOB.setText(sdf.format(myCalendar.getTime()));
        strDOB = DOB.getText().toString().trim();
        insureApplicant2.put(1, strDOB);
        passToActivity2("DOB", DOB.getText().toString().trim());
    }

activity_confirm:

//onCreate()
......
    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if(extras != null) {
            ......
            strDOB = extras.getString("ConfirmDOB");
            //Log.e("getStringDate", String.valueOf(extras.getString("confirmDOB"))); //can get value
            ......
            DOB.setText(strDOB);
            //DOB.setText(String.valueOf(extras.getString("ConfirmDOB")));  
            //DOB.setText(strDOB);
            //All return null ?!
            ......
        } else {
            ......
            DOB = null;
        }
    }
......
//End of onCreate()

......
btn_editInfo.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view){
            ......
            dataHolder.setDob(strDOB);
            ......
            //intent to main activity
        }
    });
......

dataHolder (store those editText value for get/set):

public class fragment_data_holder {
    private static data_holder mInstance;
    ......
    private String dob;
    ......

    public static data_holder getmInstance(){
        if (mInstance == null) {
            return mInstance = new data_holder();
        } else {
            return mInstance;
        }
    }

    //Getter and Setter
    ......
    public String getDob() {
        return dob;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }
}

Solution

  • Your spelling is different. On MainActivity it is "confirmDOB" while on activity_confirm it is "ConfirmDOB"