Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Add @IgnoreWhenViewDestroyed #1597

Description

@WonderCsabo

Now AndroidAnnotations clears injected view fields in onDestroyView() by setting each to null. However background tasks can be completed later. To avoid NPE-s, currently the client has to check wether the views are null or not. We could create a new annotation just like @IgnoreWhenDetached to remove that boilerplate. This was already suggested by @nbelikov.

Activity

  1. WonderCsabo commented on Oct 20, 2015

    @WonderCsabo
    MemberAuthor
  2. WonderCsabo commented on Oct 20, 2015

    @WonderCsabo
    MemberAuthor

    Or maybe we could generalize the @IgnoreWhenXXX annotations, like @IgnoreWhen(DETACHED) or @IgnoreWhen(VIEW_DESTROYED). I am not sure.

  3. dodgex commented on Oct 20, 2015

    @dodgex
    Member

    i'd prefer the @IgnoreWhen(XXX) approach.

  4. yDelouis commented on Oct 21, 2015

    @yDelouis
    Contributor

    What would be the generated code ?

  5. WonderCsabo commented on Oct 21, 2015

    @WonderCsabo
    MemberAuthor

    I thought about the following:

    boolean viewDestroyed;
    
    View onCreateView() {
      ...
      viewDestroyed = false;
    }
    
    void onDestroyView() {
      ...
      viewDestroyed = true;
    }
    
    void methodIgnoredWhenViewDestroyed() {
      if (!viewDestroyed) {
        super.methodIgnoredWhenViewDestroyed();
      }
    }
  6. yDelouis commented on Oct 21, 2015

    @yDelouis
    Contributor

    Okay. Why not.

  7. WonderCsabo commented on Oct 21, 2015

    @WonderCsabo
    MemberAuthor

    So can we rename the @IgnoreWhenDetached annotation to @IgnoreWhen and add an option to it?

  8. yDelouis commented on Oct 21, 2015

    @yDelouis
    Contributor

    Yes.

  9. self-assigned this
    on Oct 21, 2015
  10. shiraji commented on Dec 17, 2015

    @shiraji
    Contributor

    I am taking care of this one. Here is TODO

    • Create @IgnoreWhen that takes DETACHED or VIEW_DESTROYED (or DESTROY_VIEW?)
    • Based on the parameter, viewDestroyed is set to true at onDetach or onDestroyView
    • New annotation is accepted the method that has @UiThread annotation.
  11. WonderCsabo commented on Dec 17, 2015

    @WonderCsabo
    MemberAuthor

    I vote for VIEW_DESTROYED.

    viewDestroyed is only needed if VIEW_DESTROYED is used. For DETACHED, we can use the old method (getActivity() == null), there is no need to create a variable.

    @UiThread is not mandatory for this.

  12. removed their assignment
    on Dec 17, 2015
  13. shiraji commented on Dec 17, 2015

    @shiraji
    Contributor

    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 @IgnoreWhen that 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 Fragment not Activity?
    • Is VIEW_DESTROYED required to put inside Fragment or Activity?
  14. WonderCsabo commented on Dec 17, 2015

    @WonderCsabo
    MemberAuthor

    In onCreateView(), you should set the variable to false ( i guess that is a copy paste error).
    Also, this variable should be volatile (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.

  15. dodgex commented on Dec 17, 2015

    @dodgex
    Member

    New annotation is acceptable if there is at least one more annotation

    What do you mean?

    i think he means that the new @IgnoreWhen annotations 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 work

  16. shiraji commented on Dec 17, 2015

    @shiraji
    Contributor

    Oh 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();
      }
    }
  17. WonderCsabo commented on Dec 17, 2015

    @WonderCsabo
    MemberAuthor

    @shiraji this last snippet is correct.

  18. shiraji commented on Dec 17, 2015

    @shiraji
    Contributor

    Cool! Thanks for reviewing requirement, guys.

  19. shiraji commented on Dec 17, 2015

    @shiraji
    Contributor

    What about enum name?

    @IgnoreWhen(After.DETACHED)
    @IgnoreWhen(Timing.DETACHED)
    @IgnoreWhen(IgnoreWhen.DETACHED)

    I didn't come up with good one...

  20. dodgex commented on Dec 17, 2015

    @dodgex
    Member

    what about @IgnoreWhen(IgnoreWhen.State.DETACHED) or short @IgnoreWhen(State.DETACHED)

  21. dodgex commented on Dec 17, 2015

    @dodgex
    Member

    having

    @interface IgnoreWhen {
    
      //code
    
      enum State {
        // fields
      }
    }
  22. shiraji commented on Dec 17, 2015

    @shiraji
    Contributor

    I like short one!

  23. dodgex commented on Dec 17, 2015

    @dodgex
    Member

    you can do both, but if you for example have another State class you might want to use the "long" version.

  24. shiraji commented on Dec 17, 2015

    @shiraji
    Contributor

    You are right. I will put enum inside the annotation.

  25. WonderCsabo commented on Dec 17, 2015

    @WonderCsabo
    MemberAuthor

    Indeed, we should do the same as with EBean.Scope.

  26. shiraji commented on Dec 17, 2015

    @shiraji
    Contributor

    Yea. I will avoid conflict.

  27. WonderCsabo commented on Dec 20, 2015

    @WonderCsabo
    MemberAuthor

    Implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions