Since 6.0.2
@AfterCompose
@AfterCompose(superclass=true)
Target: method, class
Purpose: Marker annotation to identify a life cycle method which will be invoked in doAfterCompose() of BindComposer. Only one @AfterCompose-annotated method is allowed at the most in a ViewModel class. If you set annotation element superclass to true, the ViewModel's parent class's @AfterCompose-annotated method will be invoked first, then the child's; this logic repeats on super class. If a class has no method with @AfterCompose, no method will be called (including the super class's).[1].
For example, in a class hierarchy:
class A {
@AfterCompose
public void doA() {}
}
class B extends A {
@AfterCompose
public void doB() {}
}
class C extends B {
//no @AfterCompose method
}
class D extends C {
@AfterCompose(superclass=true)
public void doD() {}
}
class E extends D {
@AfterCompose(superclass=true)
public void doE() {}
}
E is the last child class.
-
When BindComposer reaches A, it will...
- call A's
@AfterCompose
methoddoA()
- call A's
-
When BindComposer reaches B, it will...
- call B's
@AfterCompose
methoddoB()
- A's
@AfterCompose
method will NOT be called, bacausesuperclass=true
was NOT set in B's@AfterCompose
- call B's
-
When BindComposer reaches C, it will...
- do nothing, because no method was annotated with
@AfterCompose
- do nothing, because no method was annotated with
-
When BindComposer reaches D, it will...
- since
superclass=true
, BindComposer will try to look for@AfterCompose
method in C first - no
@AfterCompose
method exists in C, so nothing will happen - call D's
@AfterCompose
methoddoD()
- since
-
When BindComposer reaches E, it will...
- find
superclass=true
, BindComposer will look for@AfterCompose
method in D first - find
superclass=true
again, BindComposer will look for@AfterCompose
method in C first - no
@AfterCompose
method exists in C, so nothing happens - call D's
@AfterCompose
methoddoD()
- call E's
@AfterCompose
methoddoE()
- find
We can also use parameter related annotation on AfterCompose method's parameters just as what we can do in @Init; please refer to subsections of Syntax/ViewModel/Parameters.
[1]: If you override parent class's @AfterCompose-annotated method and set superclass=true
, due to Java's limitation, child's method will be called twice. To avoid this, you should remove superclass=true
in child class and use super
to invoke parent's implementation.
class Parent {
@AfterCompose
public void doParent() {}
}
class WrongChild extends Parent {
@AfterCompose(superclass=true) // BindComposer will try to invoke parent's doParent()
@Override
public void doParent() {
// but since parent's method was @Override by child
// this method will be called twice
}
}
class CorrectChild extends Parent {
@AfterCompose // no superclass=true
@Override
public void doParent() {
super.doParent(); // manually invoke super class's doParent()
// child implementation of doParent()
}
}
public class FooViewModel {
@AfterCompose
public void doFoo() {
//do while AfterCompose
}
}
public class BarViewModel extends FooViewModel {
@AfterCompose(superclass=true)
public void afterComposeBar() {
//AfterCompose method of super class FooViewModel will be called first.
}
}
@AfterCompose(superclass=true)
public class ChildViewModel extends BarViewModel {
// class body
}