扩展应用
我想在我的 Android 应用程序中扩展应用程序。我已经这样做了,创建了一个名为 MyApplication 的扩展应用程序对象并将其添加到清单中。
我现在想添加一些 getter 和 setter 来保存一些信息。看起来我需要将应用程序上下文传递给任何不包含上下文的类。
例如,假设我创建一个名为 MyObject 的类(在其自己的 java 文件中):
public class MyObject {
public void doStuff() {
// do stuff
}
}
我如何从 doStuff() 访问 MyApplication?看起来我需要将应用程序上下文传递给 MyObject。这是典型的吗?有没有泄漏的可能?
另外,为了确保我清楚,MyApplication(及其中的变量)是否会在应用程序的整个生命周期中存在?无论哪种方式都可以。我只是想确保如果需要的话我会考虑到这一点。
最后,任何指向某些示例源的链接都可以显示扩展应用程序的不同用途,我们将不胜感激。
I'd like to extend Application in my Android app. I've done this such that I've created an extended Application object called MyApplication and added it to the manifest.
I'd now like to add some getters and setters to hold some information. It looks like I'll need to pass the application Context to any classes which do not contain a Context.
For example, say I create a class called MyObject (in its own java file):
public class MyObject {
public void doStuff() {
// do stuff
}
}
How might I access MyApplication from doStuff()? It looks like I'll need to pass the application Context to MyObject. Is this typical? Is there a possibility of leaks?
Also, to make sure I'm clear, will MyApplication (and the variables within) live throughout the application's lifecycle, or not? Either way is fine. I just want to make sure I account for this if I need to.
And lastly, any links to some example source to show what different things extending Application is useful for would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,传递上下文在 Android 开发中并不例外。好吧,要么那样,要么我做错了。
我不确定您是否需要这个答案,但是从上下文中,您可以像这样访问
Application
对象:您的
MyApplication
类应该存在于整个应用程序中生命周期,是的。另请查看此答案。
As far as I know, passing along contexts is not exceptional in Android development. Well, either that or I'm doing it wrong.
I'm not sure if you needed this answered or not, but from a context, you can access the
Application
object like this:Your
MyApplication
class should live throughout the entire application lifecycle, yes.Also check out this answer.
只要传递正确的上下文,在 Android 中传递 Context 就可以了。避免将“活动”作为上下文传递。请改用
getApplicationContext()
。您的 Application 对象以及与其关联的所有数据将在应用程序的整个生命周期中持续存在。即使应用程序处于后台并且所有活动都会从内存中清除,因为它们不再显示。应用程序对象何时从内存中清除并没有很好的记录,但我认为如果您的应用程序位于后台并且内存很少,操作系统可能会破坏您的整个应用程序,因此也会从内存中删除应用程序对象。
如果无法访问上下文,则无法获取应用程序对象。如果您只需要对应用程序的引用,只需传递应用程序对象,这将最大限度地减少因保存对不再在屏幕上的活动的引用并通过此引用泄漏整个活动而可能出现的错误。
我没有任何示例代码,但我用它来保存对某些任务的引用。这使我能够允许用户非常快速地在活动之间进行更改,而无需等待任务返回,即使下一个活动必须根据当前正在运行的任务更改某些状态也是如此。
Passing along Context is fine in Android as long as you pass the correct context. Avoid passing down Activities as context. Use
getApplicationContext()
instead.Your Applicationobject and all the data associated with it will persists through the whole life of your application. Even if the app is in background and all the activities get cleared from memory because they are not shown anymore. At what time the application object will be cleared from memory is not very well documented but I think that if your app is in the background and memory is rare the OS may destroy your whole app and therefore also removing the application object from memory.
There is no way to get the application object if you can't access a context. If you only need a reference to the application just pass down the application object, this will minimize the errors that can arise form saving a reference to an activity that is not on screen anymore and leaking the whole activity through this reference.
I don't have any example code but I used it to hold references to some tasks. This enabled me to allow the user to change between activities very fast without making her wait for the tasks to return even if the next activity has to change some states depending on the tasks that are running at the moment.