Android close dialog after 5 seconds?

后端 未结 8 842
北海茫月
北海茫月 2020-12-05 02:00

I\'m working on an accesibility app. When the user wants to leave the app I show a dialog where he has to confirm he wants to leave, if he doesn\'t confirm after 5 seconds t

8条回答
  •  情歌与酒
    2020-12-05 02:51

    For Kotlin inspired by Tahirhan's answer. This is what worked for my current project. Hope it will help someone else in the near future. Im calling this function in a fragment. Happy coding!

     fun showAlert(message: String) {
            val builder = AlertDialog.Builder(activity)
            builder.setMessage(message)
    
            val alert = builder.create()
            alert.show()
    
            val timer = Timer()
            timer.schedule(object : TimerTask() {
                override fun run() {
                    alert.dismiss()
                    timer.cancel()
                }
            }, 5000)
        }
    

提交回复
热议问题