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

Always call @AfterExtras methods #1528

Closed
m-radzikowski opened this issue Aug 20, 2015 · 8 comments
Closed

Always call @AfterExtras methods #1528

m-radzikowski opened this issue Aug 20, 2015 · 8 comments
Labels

Comments

@m-radzikowski
Copy link

Hey,
I've an Activity with one extra:

    @Extra
    protected Long id;

There are two use cases for this activity:

  • pass id in extra to edit existing entity,
  • don't pass any extra to open activity for new entity creation.

So I've had method:

    @AfterExtras
    protected void setupEntity() {
        if (id != null) {
            entity = loadEntityById(id);
        } else { // create new entity with default data
            entity = new Entity();
            // some more initialization of entity
        }
    }

But code generated by AA is:

    private void injectExtras_() {
        Bundle extras_ = getIntent().getExtras();
        if (extras_!= null) {
            if (extras_.containsKey(ID_EXTRA)) {
                id = ((Long) extras_.getSerializable(ID_EXTRA));
            }
            setupEntity();
        }
    }

So my setupEntity() method is not called if in result I'm unable to check if this single extra was passed or not in AA style and perform actions based on this. So I have two propositions:

  • [EASY?] change generated code to:
    private void injectExtras_() {
        Bundle extras_ = getIntent().getExtras();
        if (extras_!= null) {
            if (extras_.containsKey(ID_EXTRA)) {
                id = ((Long) extras_.getSerializable(ID_EXTRA));
            }
        }
        setupEntity();
    }
  • [HARD] add required extras to intent builder,
  • tell me what is better way to handle this without boilerplate code, AA style?

Tested on AA 3.3.2.

@dodgex
Copy link
Member

dodgex commented Aug 20, 2015

you easily can use @AfterInject or @AfterViews here. both should be called always and when they are called the extras are already injected (if available).

so simply use one of those annotations on your setupEntity() method and your app should work as intended

@dodgex
Copy link
Member

dodgex commented Aug 20, 2015

but... i just remembered... depending on how your activity might be called, this would not work when your activity gets another intent after the first. as only @AfterExtras is called for new Intents. but when you are sure that your activity is always newly created it should be no problem

@dodgex
Copy link
Member

dodgex commented Aug 20, 2015

here you can read a little bit more about when methods with an @AfterXXX annotations are called

@m-radzikowski
Copy link
Author

Ok, using @AfterInjectsolved the problem (I can't use @AfterViews in this case as I fill view depending on data from extra).

But I would still this that all @AfterXXX methods should be called on start of activity, especially when there is no mechanism to force extra set in intent. Or at least some note in documentation about @Extra annotation.

But thanks for quick response and solving my problem.

@dodgex
Copy link
Member

dodgex commented Aug 20, 2015

i'm glad i could help for now :)

@WonderCsabo @yDelouis any thoughts on calling @AfterExtras always? i think this is ok.

@yDelouis
Copy link
Contributor

I also think so. I think it's a bug.

@dodgex
Copy link
Member

dodgex commented Aug 20, 2015

i'll prepare a PR to always call @AfterExtras.

@WonderCsabo
Copy link
Member

FIxed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants