简体   繁体   English

如何从AlertDialog向活动发送消息

[英]How to Send Message From AlertDialog to Activity

I'm trying to ask the user if he wants to retry a login process if the initial one has failed. 我正在尝试询问用户,如果初始登录失败,他是否要重试登录过程。 I do this using an AlertDialog. 我使用AlertDialog进行此操作。 The problem is i'm going around in a circle. 问题是我转了一圈。 Let me explain: I have the login method named Login which I call from the main activity. 让我解释一下:我有一个名为Login的登录方法,可以从主要活动中调用该方法。 If this fails, I open an AlertDialog. 如果失败,则打开AlertDialog。 The response from AlertDialog comes on the main thread. AlertDialog的响应位于主线程上。 How do I make the AlertDialog dissapear before I call Login again? 在再次致电Login之前,如何使AlertDialog消失?

Now I have something like this onCreate->Login->AlertDialog->ResponseHandler->Login. 现在我在onCreate-> Login-> AlertDialog-> ResponseHandler-> Login中有类似的内容。 If I do this, the AlertDialog will never close because everything happens on the same thread. 如果执行此操作,则AlertDialog将永远不会关闭,因为所有事情都发生在同一线程上。

Is there a way to send the response from the AlertDialog to the activity in a asynchronous way? 是否可以通过异步方式将AlertDialog的响应发送到活动?

You can declare a Handler object as an anonymous inner class in your activity and on its reference call the sendEmptyMessage(0) . 您可以在您的活动中将Handler对象声明为匿名内部类,并在其引用上调用sendEmptyMessage(0) in the handlerMessage() of the handler instance just dismiss the AlertDialog . 在处理程序实例的handlerMessage()中,只需关闭AlertDialog

Here is a tutorial link which will help you more http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html 这是一个教程链接,可帮助您更多http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM