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.

Optionally ignore @UIThread call when @EFragment is detached from Activity? #875

Description

@vincentjames501

When using @Background in conjunction with @UIThread annotations on an @EFragment, it's possible that many developers don't want to run the method annotated with the @UIThread annotation if the Fragment is no longer attached to the activity. I think it would be great to add an annotation (something like @IgnoredWhenDetached) with the following rules:

  1. Must also have the @UIThread annotation
  2. Can only be used in conjunction with @EFragment classes
  3. Simply wraps the @UIThreadmethod call in an if block like so
if(isAdded()) {
    //executable code
}

or

if(getActivity() != null) {
    //executable code
}

Here is a simple example and in my application I have dozens of things similar to this

@EFragment
public class LoaderFragment extends Fragment {

    @Background
    void longTask() {
        try {
            updateProgress(0);
            Thread.sleep(1000);
            updateProgress(50);
            Thread.sleep(1000);
            updateProgress(100);
        } catch (InterruptedException e) {
            killActivity()        
        }
    }

    @IgnoredWhenDetached
    @UiThread
    void updateProgress(int progress) {
        getActivity().setProgress(progress);
    }

    @IgnoredWhenDetached
    @UiThread
    void killActivity(int progress) {
        getActivity().finish();
    }

This essentially prevents the two @UIThread methods from getting a NPE if the Fragment were to have been destroyed at any point during the execution of the @Background method. While I realize this is something that can be easily guarded against by a simple null check, it makes the code much cleaner and is a very common use case in conjunction with Fragments. I would be more than happy to submit a pull request pending thoughts/comments about this. This could also be useful in conjunction with @Background on an @EFragment as well, but not a very common use case.

Vince

Activity

  1. WonderCsabo commented on Jan 11, 2014

    @WonderCsabo
    Member

    Actually #823 proposed the same, but it was rejected.

  2. vincentjames501 commented on Jan 11, 2014

    @vincentjames501
    Author

    I read his actually before posting this. This is slightly different in that I'm not proposing making it the default behavior of @UIThread for @EFragment classes.

  3. WonderCsabo commented on Jan 11, 2014

    @WonderCsabo
    Member

    Yes, you are right.

  4. DayS commented on Jan 11, 2014

    @DayS
    Contributor

    Yeah, I prefer this solution as the developer still have the choice to handle theses cases as he want.
    This may be in 3.1 :)

  5. WonderCsabo commented on Jan 13, 2014

    @WonderCsabo
    Member

    I tought about the API, and bringed up a version which uses a parameter in the @UiThread and not a new annotation. But i changed my mind since this feature is only for Fragments, @UiThread API should not be affected.
    I think you can start to implement this if you want. :)

  6. yDelouis commented on Jan 13, 2014

    @yDelouis
    Contributor

    I think we could use this annotation even on methods which are not annotated with @UIThread or @Background.

  7. vincentjames501 commented on Jan 13, 2014

    @vincentjames501
    Author

    I think you may be right. I'll look at doing another pull request then let the authors decide which, if any, they would like to merge.

  8. DayS commented on Jan 13, 2014

    @DayS
    Contributor

    @yDelouis > Good idea 👍

  9. toshe commented on May 17, 2014

    @toshe
    Contributor

    This is an extremely good idea!
    I'm currently using

    if(isAdded()) {
        //executable code
    }
    

    in order to avoid the problem when switching really fast through fragments. An annotation like that makes total sense.

  10. DayS commented on Jun 8, 2014

    @DayS
    Contributor

    Merged. Thanks to @yDelouis and @vincentjames501 for this

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions