androidandroid-datepickerandroid-timepicker

Show DatePickerDialog and TimePickerDialog after one time EditText click


I tried to show DataPickerDialog and TimePickerDialog after I click EditText on AndroidViewModel.

// fragment

// in TestFragment
// pass the `requireActivity().application` to TextViewModel

// viewModel

class TestViewModel(
    application: Application,
) : AndroidViewModel(application) {
    ...
    fun showDateTimeDialog() {
        val currentDateTime = Calendar.getInstance()
        val startYear = currentDateTime.get(Calendar.YEAR)
        val startMonth = currentDateTime.get(Calendar.MONTH)
        val startDay = currentDateTime.get(Calendar.DAY_OF_MONTH)
        val startHour = currentDateTime.get(Calendar.HOUR_OF_DAY)
        val startMinute = currentDateTime.get(Calendar.MINUTE)
        DatePickerDialog(getApplication(), { _, year, month, day ->
            TimePickerDialog(getApplication(), { _, hour, minute ->
                val pickedDateTime = Calendar.getInstance()
                pickedDateTime.set(year, month, day, hour, minute)
            }, startHour, startMinute, false).show()
        }, startYear, startMonth, startDay).show()
    }
}

xml (data-binding)

        <EditText
            android:id="@+id/date_time_edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/add_date_time"
            android:importantForAutofill="no"
            android:ems="10"
            android:clickable="true"
            android:focusable="false"
            android:editable="false"
            android:onClick="@{() -> viewModel.showDateTimeDialog()}" />

But when I click this EditText, it report a error show the activity is gone while showing these two dialog.

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?


Solution

  • import android.app.Application
    import androidx.lifecycle.AndroidViewModel
    
    class TestViewModel(application: Application) : AndroidViewModel(application) {
    
        private val context = getApplication<Application>().applicationContext  
         ........
    } 
    

    Then pass context

     fun showDateTimeDialog() {
            val currentDateTime = Calendar.getInstance()
            val startYear = currentDateTime.get(Calendar.YEAR)
            val startMonth = currentDateTime.get(Calendar.MONTH)
            val startDay = currentDateTime.get(Calendar.DAY_OF_MONTH)
            val startHour = currentDateTime.get(Calendar.HOUR_OF_DAY)
            val startMinute = currentDateTime.get(Calendar.MINUTE)
            DatePickerDialog(context, { _, year, month, day ->
                TimePickerDialog(context, { _, hour, minute ->
                    val pickedDateTime = Calendar.getInstance()
                    pickedDateTime.set(year, month, day, hour, minute)
                }, startHour, startMinute, false).show()
            }, startYear, startMonth, startDay).show()
        }
    }