当只有 Context 的引用时关闭当前 Activity
如果我有对 Context 的引用,是否可以完成当前活动?
我没有当前活动的参考信息。
If I have a reference to Context
, is it possible to finish the current activity?
I don't have the reference to current activity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的,有演员阵容:
yes, with a cast:
在我的以下案例中,
我需要在 AsyncTask onPostExcute() 中完成我的活动。
我的 AsyncTask 类是单独的公共类,它有一个带有 Context 参数的构造函数。
只有上面的方法对我有用...无论如何,我从@2red13和@lucy的答案中得到了这个想法...感谢所有人...
In my Case following worked,
I need to finish my activity in a AsyncTask onPostExcute().
Where my AsyncTask class is separate public class , which has a constructor with param of Context.
Only the above worked for me... Anyway I got this idea from @2red13 and @lucy answers... Thanks to all...
我知道这是一篇旧文章,但是,也许这样称呼它是一个好主意:
这样我们可以确保我们不会得到任何不必要的 ClassCastExceptions
I know it's an old post but, perhaps it could be a good idea to call it this way:
This way we make sure we don't get any unnecesary ClassCastExceptions
如果您有权访问要完成的 Activity 的运行视图(例如,您位于单击侦听器中),您可以执行以下操作:(
感谢 2red13 让我来到这里)。
If you have access to the running view of the Activity you want to finish (for example, you are in a click listener), you could do:
(With thanks to 2red13 to get me here).
如果您使用以下方式启动活动:
您可以调用 finishActivity(1) 来完成使用该请求代码启动的任何活动,如下所示:
在我的情况下,我需要在处理程序 postDelayed 中使用一个活动。使用此功能,您可以确定您正在完成哪项活动。希望有帮助!
If you start the activity using:
you can call finishActivity(1) to finish any activities started with that request code, like this:
In my case I need to use one in a handler postDelayed. Using this you can be sure of which activity you are finishing. Hope it helps!
关闭偏好活动时我遇到了同样的问题。这是我所做的:
I had the same problem when closing a preference activity. Here is what I did:
尝试:
Try: