Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

316
Views
¿El hecho de no llamar a Continuation.resumeX() es necesariamente un problema?

Estoy usando suspendCoroutine para evitar el uso de devoluciones de llamada en Dialog s. Sin embargo, en Android Dialog s no hay un lugar obvio para llamar a Continuation.resume() cuando se descarta el diálogo (haciendo clic fuera del área de diálogo). Si intenta la llamada en Dialog.setOnDismissListener() , entonces debe realizar un seguimiento de si ya se llamó a resume en el botón de escucha.

 suspend fun displayDialog() = suspendCoroutine<String?> { continuation -> val builder = AlertDialog.Builder(context) builder.setCancelable(true) builder.setNegativeButton(android.R.string.cancel) { _, _ -> continuation.resume(null) } builder.setPositiveButton(android.R.string.ok) { _, _ -> continuation.resume("it's ok") } val dialog = builder.show() dialog.setOnDismissListener { // if the user clicked on OK, then resume has already been called // and we get an IllegalStateException continuation.resume(null) } }

Entonces, ¿es mejor hacer un seguimiento de si ya se llamó a resume (para evitar llamarlo por segunda vez), o simplemente no molestarse con la llamada resume(null) (en onDismissListener)?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

La continuación es una primitiva de bajo nivel que se reanudará exactamente una vez, por lo que debe realizar un seguimiento de si ya se llamó a resume al usarlo. Alternativamente, puede usar primitivas de comunicación de nivel superior, por ejemplo CompletableDeferred , que tiene una función complete de usos múltiples:

 suspend fun displayDialog(): String? { val deferred = CompletableDeferred<String?>() val builder = AlertDialog.Builder(context) builder.setCancelable(true) builder.setNegativeButton(android.R.string.cancel) { _, _ -> deferred.complete(null) } builder.setPositiveButton(android.R.string.ok) { _, _ -> deferred.complete("it's ok") } val dialog = builder.show() dialog.setOnDismissListener { deferred.complete(null) } return deferred.await() }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!