我想在我的Android应用程序扩展应用。我已经做到了这一点,这样我已经创建了一个名为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.
我现在想添加一些getter和setter方法来保存一些信息。它看起来像我需要应用程序上下文传递给它不包含任何上下文类。
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.
举例来说,假设我创建了一个类叫MyObject的(在它自己的Java文件):
For example, say I create a class called MyObject (in its own java file):
public class MyObject {
public void doStuff() {
// do stuff
}
}
我怎么可能从doStuff访问所有MyApplication()?它看起来像我需要的应用程序上下文传递给MyObject来。这是典型的?是否有泄漏的可能?
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?
此外,为确保我清楚,将所有MyApplication(和内部变量)住在整个应用程序的生命周期,还是没有?无论哪种方式是罚款。我只是想确保我考虑到这一点,如果我需要。
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.
最后,一些示范来源的任何链接,展示一下不同的东西将AP preciated扩展应用是很有用的。
And lastly, any links to some example source to show what different things extending Application is useful for would be appreciated.
推荐答案
据我所知,还有上下文传球也不例外在Android开发。好了,如果不是这样,我是做错了。
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:
((MyApplication) context.getApplicationContext()).getMyGlobalVariable();
您所有MyApplication
类应该生活在整个应用程序生命周期,是的。
Your MyApplication
class should live throughout the entire application lifecycle, yes.
另外,请查阅this回答。
相关推荐
最新文章