Repository navigation
Add @IgnoreWhenViewDestroyed #1597
Description
Activity
Or maybe we could generalize the
@IgnoreWhenXXXannotations, like@IgnoreWhen(DETACHED)or@IgnoreWhen(VIEW_DESTROYED). I am not sure.i'd prefer the
@IgnoreWhen(XXX)approach.What would be the generated code ?
I thought about the following:
boolean viewDestroyed; View onCreateView() { ... viewDestroyed = false; } void onDestroyView() { ... viewDestroyed = true; } void methodIgnoredWhenViewDestroyed() { if (!viewDestroyed) { super.methodIgnoredWhenViewDestroyed(); } }
Okay. Why not.
So can we rename the
@IgnoreWhenDetachedannotation to@IgnoreWhenand add an option to it?Yes.
I am taking care of this one. Here is TODO
- Create
@IgnoreWhenthat takes DETACHED or VIEW_DESTROYED (or DESTROY_VIEW?) - Based on the parameter, viewDestroyed is set to true at
onDetachoronDestroyView - New annotation is accepted the method that has
@UiThreadannotation.
- Create
I vote for
VIEW_DESTROYED.viewDestroyedis only needed ifVIEW_DESTROYEDis used. ForDETACHED, we can use the old method (getActivity() == null), there is no need to create a variable.@UiThreadis not mandatory for this.Could you check if I understand this issue correctly?
Generated Code
@IgnoreWhen(DETACHED)void methodIgnoredWhenViewDestroyed() { if (getActivity() != null) { super.methodIgnoredWhenViewDestroyed(); } }
@IgnoreWhen(VIEW_DESTROYED)boolean viewDestroyed; View onCreateView() { ... viewDestroyed = false; } void onDestroyView() { ... viewDestroyed = true; } void methodIgnoredWhenViewDestroyed() { if (!viewDestroyed) { super.methodIgnoredWhenViewDestroyed(); } }
TODO
- Create
@IgnoreWhenthat takes DETACHED or VIEW_DESTROYED - Based on the parameter, generated codes are above.
- New annotation is acceptable if there is at least one more annotation.
Quetion
- No default value for this annotation?
- Is DETACHED required to put inside
FragmentnotActivity? - Is VIEW_DESTROYED required to put inside
FragmentorActivity?
- Create
In
onCreateView(), you should set the variable tofalse( i guess that is a copy paste error).
Also, this variable should bevolatile(since the decorated method maybe does not run on the main thread).New annotation is acceptable if there is at least one more annotation
What do you mean?
There is no default value.
This annotation can be only used in@EFragments.New annotation is acceptable if there is at least one more annotation
What do you mean?
i think he means that the new
@IgnoreWhenannotations requires at least one other annotation like@UiThread. but i'd say no. this annotation should also work. a method annotated with this could be called from any thread/method so it should be ensured that it always workOh I got it. Thanks @dodgex
I'm not sure about this
In onCreateView(), you should set the variable to false ( i guess that is a copy paste error).
private volatile boolean viewDestroyed; View onCreateView() { ... viewDestroyed = false; } void onDestroyView() { ... viewDestroyed = true; } void methodIgnoredWhenViewDestroyed() { if (!viewDestroyed) { super.methodIgnoredWhenViewDestroyed(); } }
@shiraji this last snippet is correct.
Cool! Thanks for reviewing requirement, guys.
What about enum name?
@IgnoreWhen(After.DETACHED)
@IgnoreWhen(Timing.DETACHED)
@IgnoreWhen(IgnoreWhen.DETACHED)I didn't come up with good one...
what about
@IgnoreWhen(IgnoreWhen.State.DETACHED)or short@IgnoreWhen(State.DETACHED)having
@interface IgnoreWhen { //code enum State { // fields } }
I like short one!
you can do both, but if you for example have another
Stateclass you might want to use the "long" version.You are right. I will put enum inside the annotation.
Indeed, we should do the same as with
EBean.Scope.Yea. I will avoid conflict.
Implemented.
Now AndroidAnnotations clears injected view fields in
onDestroyView()by setting each tonull. However background tasks can be completed later. To avoid NPE-s, currently the client has to check wether the views arenullor not. We could create a new annotation just like@IgnoreWhenDetachedto remove that boilerplate. This was already suggested by @nbelikov.