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.

Lifespan validation for @UiThread #1874

Description

@couchcrew-thomas

This is not a bug but a discussion of a feature request.

TL;DR extend AA to execute methods only in certain life-cycle states

In my code I have to repeatedly check if a method annotated with @UiThread is not being called after the activity / fragment has been destroyed. Most of the time this occurs when a long running background operation returns to display its result.

@UiThread
protected void foo() {
  if (!isResumed()) {
     return;
  }
  // [...]
}

To prevent to write this check manually every time my idea was to add a parameter lifespan to the @UiThread annotation (Or whatever because I'm bad at naming things). Its values could be the following:

enum LIFESPAN {
    CREATED, //valid between onCreate and onDestroy
    STARTED, //valid between onStart and onStop
    RESUMED //valid between onResume and onPause
};

An annotated method would look this way:

@UiThread(lifespan = RESUMED)
protected void foo() {
  // [...] executed when activity / fragment is in resumed state
}

The generated class would have to override the onCreate... lifecycle methods to save the current state and query this state inside the generated methods that use the lifespan parameter.

The downside of this idea is that the parameter could only be used in activities or fragments and as a result the @UiThread annotation would not have the same features in all @E... anymore.
An alternate approach would be to create a new annotation similar to @IgnoreWhen that checks for the lifecycle states in @EActivity and @EFragment.

What is your opinion on this matter?

Activity

  1. Thohid commented on Oct 13, 2016

    @Thohid

    @UiThread

  2. Thohid commented on Oct 13, 2016

    @Thohid

    @UiThread
    protected void foo() {
    if (!isResumed()) {
    return;
    }
    // [...]
    }

  3. WonderCsabo commented on Nov 23, 2016

    @WonderCsabo
    Member

    @couchcrew-thomas i would use a different annotation. Maybe we can extend @IgnoreWhen. @dodgex wdyt?

  4. dodgex commented on Nov 23, 2016

    @dodgex
    Member

    I think @IgnoreWhen is a good idea. this allows to be used on other methods than @UiThread too. e.g. networking callbacks

  5. WonderCsabo commented on Nov 23, 2016

    @WonderCsabo
    Member

    @couchcrew-thomas contributions are always welcome!

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