Skip to content

Commit 14a8757

Browse files
LochnairWonderCsabo
authored andcommitted
Add forceLayoutInjection to EFragment
1 parent 1c146db commit 14a8757

3 files changed

Lines changed: 32 additions & 6 deletions

File tree

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
/**
2424
* <p>
25-
* Should be used on android.app.Fragment or
26-
* link android.support.v4.app.Fragment classes to enable usage of
25+
* Should be used on android.app.Fragment or link
26+
* android.support.v4.app.Fragment classes to enable usage of
2727
* AndroidAnnotations.
2828
* </p>
2929
* <p>
@@ -46,6 +46,11 @@
4646
* <code>onCreateView()</code>.
4747
* </p>
4848
* <p>
49+
* We only set the layout if the <code>onCreateView()</code> method returns null
50+
* from the annotated class. If you want to set the layout regardless of that
51+
* return value, use the {@link #forceLayoutInjection()} annotation parameter.
52+
* </p>
53+
* <p>
4954
* The generated class will also contain a FragmentBuilder to build fragment
5055
* with a fluent API. Arguments can be passed by using {@link FragmentArg}
5156
* annotation on every native or serializable/parcelable field.
@@ -106,4 +111,13 @@
106111
int value() default ResId.DEFAULT_VALUE;
107112

108113
String resName() default "";
114+
115+
/**
116+
* We only set the layout if the <code>onCreateView()</code> method returns
117+
* null from the annotated class. If you want to set the layout regardless
118+
* of that return value, pass <b>true</b> to this annotation parameter.
119+
*
120+
* @return <b>true</b>, if the layout must be set, <b>false</b> otherwise
121+
*/
122+
boolean forceLayoutInjection() default false;
109123
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/EFragmentHandler.java‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,16 @@ public void process(Element element, EFragmentHolder holder) {
7474

7575
JFieldVar contentView = holder.getContentView();
7676

77-
block._if(contentView.eq(_null())) //
78-
._then() //
79-
.assign(contentView, inflater.invoke("inflate").arg(contentViewId).arg(container).arg(FALSE));
77+
boolean forceInjection = element.getAnnotation(EFragment.class).forceLayoutInjection();
78+
79+
if (!forceInjection) {
80+
block._if(contentView.eq(_null())) //
81+
._then() //
82+
.assign(contentView, inflater.invoke("inflate").arg(contentViewId).arg(container).arg(FALSE));
83+
} else {
84+
block.assign(contentView, inflater.invoke("inflate").arg(contentViewId).arg(container).arg(FALSE));
85+
}
86+
8087
}
8188

8289
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/EFragmentHolder.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.sun.codemodel.JMod;
2828
import com.sun.codemodel.JTypeVar;
2929
import com.sun.codemodel.JVar;
30+
import org.androidannotations.annotations.EFragment;
3031
import org.androidannotations.helper.ActionBarSherlockHelper;
3132
import org.androidannotations.helper.AnnotationHelper;
3233
import org.androidannotations.process.ProcessHolder;
@@ -247,8 +248,12 @@ private void setOnCreateView() {
247248

248249
JVar savedInstanceState = onCreateView.param(classes().BUNDLE, "savedInstanceState");
249250

251+
boolean forceInjection = getAnnotatedElement().getAnnotation(EFragment.class).forceLayoutInjection();
252+
250253
JBlock body = onCreateView.body();
251-
body.assign(contentView, _super().invoke(onCreateView).arg(inflater).arg(container).arg(savedInstanceState));
254+
255+
if (!forceInjection)
256+
body.assign(contentView, _super().invoke(onCreateView).arg(inflater).arg(container).arg(savedInstanceState));
252257

253258
setContentViewBlock = body.block();
254259

0 commit comments

Comments
 (0)