使用RoboGuice 2.0的Android应用程序 - 如何使用Application上下文注入Singleton

时间:2022-09-25 10:58:55

I have a GameStateManager singleton that I want to have available to all of my activities. In particular I want it to listen for Events fired off with the EventManager using the Application Context instead of an individual Activity Context.

我有一个GameStateManager单例,我想让我的所有活动都可用。特别是我希望它使用Application Context而不是单独的Activity Context来监听使用EventManager触发的事件。

GameStateManager is marked with the singleton annotation

GameStateManager标有单例注释

I tried to inject GameStateManager during Application.OnCreate (sorry, typed the below snippet from memory, not copied and pasted, so might be incorrect)

我试着在Application.OnCreate期间注入GameStateManager(抱歉,从内存中输入以下代码片段,没有复制和粘贴,所以可能不正确)

public void OnCreate(){
    GameStateManager gameStateManager = RoboGuice.InjectMembers(this.getApplicationContext(), new GameStateManager())

}

I thought that the instance of GameStateManager would be constructed with the application context and since it is annotated as a singleton would therefore be available later with the application context. What I noticed was that when I injected the GameStateManager into an activity, I actually got a new singleton tied to the activity context. So in essence I have 2 singletons :)

我认为GameStateManager的实例将使用应用程序上下文构造,因为它被注释为单例,因此稍后可以使用应用程序上下文。我注意到的是,当我将GameStateManager注入到一个活动中时,我实际上得到了一个与活动上下文相关联的新单例。所以本质上我有2个单身:)

Any ideas on how to have a true 'singleton' that is connected to the Application context?

关于如何拥有一个连接到Application上下文的真正“单例”的任何想法?

Thanks!

2 个解决方案

#1


1  

The issue you observe could be caused by lazy initialization (see https://code.google.com/p/google-guice/wiki/Scopes) in the development mode.

您观察到的问题可能是由于开发模式下的延迟初始化(请参阅https://code.google.com/p/google-guice/wiki/Scopes)。

If you inject your manager at first in an activity, it is created lazily at that point. Since Activity satisfies for any @Inject Context, the activity is injected. This is actually very harmful, because if your manager is annotated with @Singleton, it lives longer than the activity and you basically just created a memory leak.

如果您最初在活动中注入经理,则会在此时懒惰地创建。由于Activity满足任何@Inject Context,因此会注入活动。这实际上是非常有害的,因为如果您的经理使用@Singleton注释,它的活动时间比活动长,并且您基本上只是创建了内存泄漏。

I found it more explicit to @Inject Application or Activity depending on what I expected to be injected where (Activity usually for @ContextSingleton's, Application for plain @Singleton's).

我发现@Inject应用程序或Activity更明确,这取决于我期望注入的位置(Activity通常用于@ ContextSingleton,应用程序用于普通@ Singleton的)。

#2


0  

Since RoboGuice is built under Guice you could try to use @Singelton annotation which guarantee one instance per Injector

由于RoboGuice是在Guice下构建的,因此您可以尝试使用@Singelton注释,该注释可为每个Injector保证一个实例

Take a look to example application

看看示例应用程序

#1


1  

The issue you observe could be caused by lazy initialization (see https://code.google.com/p/google-guice/wiki/Scopes) in the development mode.

您观察到的问题可能是由于开发模式下的延迟初始化(请参阅https://code.google.com/p/google-guice/wiki/Scopes)。

If you inject your manager at first in an activity, it is created lazily at that point. Since Activity satisfies for any @Inject Context, the activity is injected. This is actually very harmful, because if your manager is annotated with @Singleton, it lives longer than the activity and you basically just created a memory leak.

如果您最初在活动中注入经理,则会在此时懒惰地创建。由于Activity满足任何@Inject Context,因此会注入活动。这实际上是非常有害的,因为如果您的经理使用@Singleton注释,它的活动时间比活动长,并且您基本上只是创建了内存泄漏。

I found it more explicit to @Inject Application or Activity depending on what I expected to be injected where (Activity usually for @ContextSingleton's, Application for plain @Singleton's).

我发现@Inject应用程序或Activity更明确,这取决于我期望注入的位置(Activity通常用于@ ContextSingleton,应用程序用于普通@ Singleton的)。

#2


0  

Since RoboGuice is built under Guice you could try to use @Singelton annotation which guarantee one instance per Injector

由于RoboGuice是在Guice下构建的,因此您可以尝试使用@Singelton注释,该注释可为每个Injector保证一个实例

Take a look to example application

看看示例应用程序