以静态方式访问 Activity
我有一个活动,它有一个用于更新文本字段
的静态方法。这样我就可以从另一个活动更新此视图。
但现在我试图在这个静态方法中获取上下文变量,这是不可能的。我尝试声明一个 Context 变量并在 onCreate ( context = getApplicationContext();) 中初始化它,
但我仍然无法在此静态方法中访问上下文。这通常是如何完成的?
编辑:关于我的情况的更多信息。我正在一个活动 (a) 中启动一个倒计时器,它会每秒更新另一个活动的(b)“文本字段”。它通过以静态方式访问 b 的 setTextField
来实现这一点。
I have an activity which has a static method for updating a textfield
. This way I can update this view from another activity.
But now I'm trying to get a Context variable in this static method which is not possible. I've tried declaring a Context variable and initialising it in onCreate ( context = getApplicationContext();)
But still I can't access context in this static method. How is this normally done?
edit: a little bit more information about my situation. I'm starting a countdowntimer
in an activity(a) which updates another activity's(b) ``textfield every second. And it does this by accessing b's setTextField
in a static way..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
通过静态方法访问
TextView
并不是从另一个活动更新字段的最佳方法。如果您想在活动启动时向其传递一个值,您可以通过 Intent(即intent.getExtras
)发送数据。如果您想从子活动传回数据,可以使用startActivityForResult。Accessing a
TextView
via a static method is not the best way to update the field from another activity. If you want to pass a value to the activity when it starts, you can send data via the intent (i.e.intent.getExtras
). If you want to pass data back from a sub-activity, you can usestartActivityForResult
.你要走的路很奇怪。您为什么要尝试改变一项活动的内容?可能您需要使用
startActivityForResult
来构建一个新活动,然后从中返回结果并根据它更改视图?The way you are going is very strange. Why are you trying to change one activity content from another? May be you need to use
startActivityForResult
to strat a new activity and then return result from it and change views depending on it?您可能想查看一些有关 OO 和使用静态函数的文档。这不被认为是一个很好的方法。
但由于我们不是在谈论更好的完整解决方案:您可以向函数添加一个带有上下文的参数,并在调用函数时给出它:)
You might want to check some documentation on OO and using static functions. It is not considered a very good approach.
But as we are not talking about a better complete sollution: you can add a parameter with a context to the function, and just give it when you call the function :)
我建议使用 LocalBinder 模式来更新其他 Activity:
http://developer.android .com/resources/samples/ApiDemos/src/com/example/android/apis/app/LocalService.html
I would suggest the LocalBinder pattern to update the other Activity:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LocalService.html
你能做这样的事情吗?
像这样的.getContext()
参考:如何从非 Activity 类启动 Activity?
Can you do something like this?
something like this
<viewobj>.getContext()
Ref: How can I start an Activity from a non-Activity class?
每当您忙于活动 A 时,就没有必要更新活动 B 上的某些内容,因为当时它根本不会向用户显示。
在我看来,您需要在这里有某种全局变量,可以在 Activity B 的 onResume 中获取该变量。
查看这个问题: 如何在 Android 中声明全局变量?
它向您展示了如何使用 Application 类来维护全局应用程序状态,以便在需要时可从所有 Activity 访问。
Whenever you're busy with Activity A, there's no point in updating something on Activity B as it is simply not shown to the user at that point in time.
Seems to me you need to have some kind of global variable here that can be picked up in the onResume of Activity B.
Checkout this question : How to declare global variables in Android?
It shows you how to use the Application class to maintain global application state, accessable from all activities when needed.