From 27dafde5a487e7e5ff8d276a88d141e892af1eaa Mon Sep 17 00:00:00 2001 From: Kay-Uwe Janssen Date: Wed, 23 Nov 2016 18:22:55 +0100 Subject: [PATCH] Do not clear views when injected via method --- .../test/efragment/MySupportFragment.java | 8 ++++++++ .../internal/core/handler/ViewByIdHandler.java | 9 ++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/efragment/MySupportFragment.java b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/efragment/MySupportFragment.java index 1de6d6dfb2..8dbc3db1d5 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/efragment/MySupportFragment.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/efragment/MySupportFragment.java @@ -1,5 +1,6 @@ /** * Copyright (C) 2010-2016 eBusiness Information, Excilys Group + * Copyright (C) 2016 the AndroidAnnotations project * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of @@ -73,4 +74,11 @@ void calledAfterViewInjection() { } + @ViewById(R.id.my_text_view) + void methodView1(TextView methodView1) { + } + + void methodView2(@ViewById(R.id.my_text_view) TextView methodView2) { + } + } diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/ViewByIdHandler.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/ViewByIdHandler.java index f4b9088f89..56d9ac0ac7 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/ViewByIdHandler.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/ViewByIdHandler.java @@ -1,5 +1,6 @@ /** * Copyright (C) 2010-2016 eBusiness Information, Excilys Group + * Copyright (C) 2016 the AndroidAnnotations project * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of @@ -19,6 +20,7 @@ import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; +import javax.lang.model.element.ExecutableElement; import javax.lang.model.type.TypeMirror; import org.androidannotations.AndroidAnnotationsEnvironment; @@ -67,12 +69,17 @@ public void validate(Element element, ElementValidation validation) { @Override public void process(Element element, EComponentWithViewSupportHolder holder) { injectHelper.process(element, holder); - if (holder instanceof EFragmentHolder) { + if (holder instanceof EFragmentHolder && isFieldInjection(element)) { String fieldName = element.getSimpleName().toString(); ((EFragmentHolder) holder).clearInjectedView(ref(fieldName)); } } + private boolean isFieldInjection(Element element) { + Element enclosingElement = element.getEnclosingElement(); + return !((element instanceof ExecutableElement) || (enclosingElement instanceof ExecutableElement)); + } + @Override public JBlock getInvocationBlock(EComponentWithViewSupportHolder holder) { return holder.getOnViewChangedBodyInjectionBlock();