In the previous article, we briefly introduced the use of roboguice ([5] use of the injection framework roboguice :( your first pojo injection). Today we will look at the Order example and context Singleton (contextsingletons) using the annotation method, I believe you are familiar with the annotation of common Java objects (pojo) from the previous article.
(1) Overview: brief usage
①: Create an activity class that inherits the roboactivity
②: Use @ inject to annotate pojo (common Java object)
③: Use @ Singleton to annotate the pojo class
(2) Next we will implement a singleton Annotation
<span style="font-size:18px;">class MyActivity extends RoboActivity { @Inject Foo foo; // this will basically call new Foo();}</span>In the example of the code above, we know that every time a myactivity instance is not created, a new Foo instance will be created.
If @ Singleton is used to annotate the foo class, roboguice will initialize only one Foo instance and the same instance.
<span style="font-size:18px;">@Singleton //a single instance of Foo is now used though the whole appclass Foo {}</span>In this case
<pre name="code" class="java">new MyRoboActivity().foo = new MyRoboActivity().foo
(3) Note: When you use @ Singleton for annotation, you will create an object not collected by the garbage collection period. This object is destroyed only when the application itself is destroyed. Even if your activity does not apply to it, the object will remain in the content as long as the application is still running. For this reason, if we do not have it correctly, memory leakage may occur. To solve this problem, we can use the following context Singleton.
(4) Context singletons
Compared with the @ Singleton annotation, @ contextsingleton is used to annotate the lifecycle of a created Ticket Based on the context, and then the garbage collection period (GC) is recycled. The following is how to use it:
@ContextSingleton //a single instance of Foo is now used per contextclass Foo {}In the preceding example, Foo will create only one instance in the Context range. This means that the two myactivity instances will have two different Foo instances (opposite to the @ Singleton annotation, in which case a single meeting will be shared in activities ). But in the same context, foo has and will only be instantiated once (opposite to @ Singleton or @ contextsingleton, in this case, a different Foo instance is created for each annotation ).
For details, see:
public MyActivity extends RoboActivity { @Inject Foo foo; @Inject Bar bar;}public class Foo { @Inject Bar bar;}@ContextSingletonpublic class Bar {}In this case:
new MyRoboActivity().foo != new MyRoboActivity().fooMyRoboActivity a = new MyRoboActivity();a.bar == a.foo.bar
[Note] when you use the @ contextsingleton annotation, the object you create will not be recycled in the context lifecycle. This will be destroyed when the context is destroyed, but if your context is not used, it will still exist in the memory. That is to say, if you do not use @ contextsingleton correctly, there will still be content leakage. For example, when fragments is used. See @ fragmentsingleton below.
(5): roboguice 3.0
In roboguice 3.0, a class fragmentscope is added. java. This range spans the entire fragment lifecycle. Annotations within this range will exist along with the fragment lifecycle and will be destroyed only when fragment is destroyed.
Using fragmentscope, we can also define a singleton. Each fragmentsingleton will be instantiated only once in fragmentscope.
public MyFragment extends RoboFragment { @Inject Foo foo; @Inject Bar bar;}public class Foo { @Inject Bar bar;}@FragmentSingletonpublic class Bar {}In the above case, each annotation field: @ inject Foo has different Foo instance objects. In contrast, @ inject bar annotation only obtains the same bar instance object in the same fragmentscope.
For example, in the above example.
myFragment.bar = myFragment.foo.barnew MyFragment().bar = new MyFragment().foo.bar
[6] use of the injection framework roboguice: (singletons and contextsingletons)