Skip to content

Commit 3ff98e4

Browse files
committed
Add Activity and Fragment scope for the @ebean annotation.
1 parent eeffe64 commit 3ff98e4

25 files changed

Lines changed: 627 additions & 90 deletions

File tree

‎AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/annotations/EBean.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,22 @@ enum Scope {
110110
*/
111111
Default, //
112112

113+
/**
114+
* A new instance of the bean is created the first time it is needed in an
115+
* Activity scope, it is then retained and the same instance is always returned
116+
* from within the same Activity. Different Activities hold different copies of
117+
* the bean.
118+
*/
119+
Activity,
120+
121+
/**
122+
* A new instance of the bean is created the first time it is needed in a
123+
* Fragment scope, it is then retained and the same instance is always returned
124+
* from within the same Fragment. Different Fragments hold different copies of
125+
* the bean.
126+
*/
127+
Fragment,
128+
113129
/**
114130
* A new instance of the bean is created the first time it is needed, it is then
115131
* retained and the same instance is always returned.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (C) 2016-2019 the AndroidAnnotations project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.api.bean;
17+
18+
public interface BeanHolder {
19+
20+
<T> T getBean(Class<T> key);
21+
22+
<T> void putBean(Class<T> key, T value);
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright (C) 2016-2019 the AndroidAnnotations project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.ebean;
17+
18+
import org.androidannotations.annotations.EBean;
19+
20+
@EBean(scope = EBean.Scope.Activity)
21+
public class ActivityScopedBean {
22+
}

‎AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/BeanInjectedActivity.java‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,29 @@ public class BeanInjectedActivity extends Activity {
3333
@Bean
3434
public SomeSingleton singletonDependency;
3535

36+
@Bean
37+
public ActivityScopedBean activityScopedDependency;
38+
39+
@Bean
40+
public FragmentScopedBean fragmentScopedDependency;
41+
3642
public EmptyDependency methodInjectedDependency;
37-
public SomeSingleton methodInjectedSingleton;
3843
public SomeInterface methodInjectedInterface;
44+
public SomeSingleton methodInjectedSingleton;
45+
public ActivityScopedBean methodInjectedActivityScoped;
46+
public FragmentScopedBean methodInjectedFragmentScoped;
3947

4048
public EmptyDependency annotatedParamDependency;
41-
public SomeSingleton annotatedParamSingleton;
4249
public SomeInterface annotatedParamInterface;
50+
public SomeSingleton annotatedParamSingleton;
51+
public ActivityScopedBean annotatedParamActivityScoped;
52+
public FragmentScopedBean annotatedParamFragmentScoped;
4353

4454
public EmptyDependency multiDependency;
45-
public SomeSingleton multiDependencySingleton;
4655
public SomeInterface multiDependencyInterface;
56+
public SomeSingleton multiDependencySingleton;
57+
public ActivityScopedBean multiDependencyActivityScopedBean;
58+
public FragmentScopedBean multiDependencyFragmentScopedBean;
4759

4860
@Bean
4961
protected void injectDependency(EmptyDependency methodInjectedDependency) {
@@ -60,6 +72,16 @@ protected void injectSingleton(SomeSingleton methodInjectedSingleton) {
6072
this.methodInjectedSingleton = methodInjectedSingleton;
6173
}
6274

75+
@Bean
76+
protected void injectActivityScope(ActivityScopedBean methodInjectedActivityScoped) {
77+
this.methodInjectedActivityScoped = methodInjectedActivityScoped;
78+
}
79+
80+
@Bean
81+
protected void injectFragmentScope(FragmentScopedBean methodInjectedFragmentScoped) {
82+
this.methodInjectedFragmentScoped = methodInjectedFragmentScoped;
83+
}
84+
6385
protected void injectDependencyAnnotatedParam(@Bean EmptyDependency annotatedParamDependency) {
6486
this.annotatedParamDependency = annotatedParamDependency;
6587
}
@@ -72,10 +94,20 @@ protected void injectSingletonAnnotatedParam(@Bean SomeSingleton annotatedParamS
7294
this.annotatedParamSingleton = annotatedParamSingleton;
7395
}
7496

97+
protected void injectActivityScopedAnnotatedParam(@Bean ActivityScopedBean annotatedParamActivityScoped) {
98+
this.annotatedParamActivityScoped = annotatedParamActivityScoped;
99+
}
100+
101+
protected void injectFragmentScopedAnnotatedParam(@Bean FragmentScopedBean annotatedParamFragmentScoped) {
102+
this.annotatedParamFragmentScoped = annotatedParamFragmentScoped;
103+
}
104+
75105
protected void injectMultipleDependencies(@Bean EmptyDependency multiDependency, @Bean(SomeImplementation.class) SomeInterface multiDependencyInterface,
76-
@Bean SomeSingleton multiDependencySingleton) {
106+
@Bean SomeSingleton multiDependencySingleton, @Bean ActivityScopedBean multiDependencyActivityScopedBean, @Bean FragmentScopedBean multiDependecyFragmentScopedBean) {
77107
this.multiDependency = multiDependency;
78108
this.multiDependencyInterface = multiDependencyInterface;
79109
this.multiDependencySingleton = multiDependencySingleton;
110+
this.multiDependencyActivityScopedBean = multiDependencyActivityScopedBean;
111+
this.multiDependencyFragmentScopedBean = multiDependecyFragmentScopedBean;
80112
}
81113
}

‎AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/CustomFragment.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,13 @@ public class CustomFragment extends Fragment {
3232
@Bean
3333
SomeBeanWithRootFragmentWithDifferentFragment someBeanWithDifferentFragment;
3434

35+
@Bean
36+
ActivityScopedBean activityScopedBean;
37+
38+
@Bean
39+
FragmentScopedBean fragmentScopedBean1;
40+
41+
@Bean
42+
FragmentScopedBean fragmentScopedBean2;
43+
3544
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright (C) 2016-2019 the AndroidAnnotations project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.ebean;
17+
18+
import org.androidannotations.annotations.Bean;
19+
import org.androidannotations.annotations.EFragment;
20+
21+
import android.app.Fragment;
22+
23+
@EFragment
24+
public class CyclicBeansFragment extends Fragment {
25+
26+
@Bean
27+
SomeCyclicSingletonA singletonA;
28+
29+
@Bean
30+
SomeCyclicSingletonB singletonB;
31+
32+
@Bean
33+
SomeCyclicActivityScopedA cyclicActivityScopedA;
34+
35+
@Bean
36+
SomeCyclicActivityScopedB cyclicActivityScopedB;
37+
38+
@Bean
39+
SomeCyclicFragmentScopedA cyclicFragmentScopedA;
40+
41+
@Bean
42+
SomeCyclicFragmentScopedB cyclicFragmentScopedB;
43+
44+
}

‎AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/DifferentFragment.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@
1515
*/
1616
package org.androidannotations.test.ebean;
1717

18+
import org.androidannotations.annotations.Bean;
1819
import org.androidannotations.annotations.EFragment;
1920

2021
import android.app.Fragment;
2122

2223
@EFragment
2324
public class DifferentFragment extends Fragment {
2425

26+
@Bean
27+
ActivityScopedBean activityScopedBean;
28+
29+
@Bean
30+
FragmentScopedBean fragmentScopedBean;
31+
2532
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright (C) 2016-2019 the AndroidAnnotations project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.ebean;
17+
18+
import org.androidannotations.annotations.EBean;
19+
20+
@EBean(scope = EBean.Scope.Fragment)
21+
public class FragmentScopedBean {
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (C) 2016-2019 the AndroidAnnotations project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.ebean;
17+
18+
import org.androidannotations.annotations.Bean;
19+
import org.androidannotations.annotations.EBean;
20+
21+
@EBean(scope = EBean.Scope.Activity)
22+
public class SomeCyclicActivityScopedA {
23+
24+
@Bean
25+
SomeCyclicActivityScopedB activityScopedB;
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (C) 2016-2019 the AndroidAnnotations project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.ebean;
17+
18+
import org.androidannotations.annotations.Bean;
19+
import org.androidannotations.annotations.EBean;
20+
21+
@EBean(scope = EBean.Scope.Activity)
22+
public class SomeCyclicActivityScopedB {
23+
24+
@Bean
25+
SomeCyclicActivityScopedA activityScopedA;
26+
27+
}

0 commit comments

Comments
 (0)