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

Determine if activity/fragment is firstly created or re-created in @AfterViews #1947

Open
fikr4n opened this issue Jan 30, 2017 · 3 comments

Comments

@fikr4n
Copy link

fikr4n commented Jan 30, 2017

Description of the feature

Sometimes we need to do something based on whether the activity/fragment is firstly created or re-created (e.g. configuration changes). Usually I check it in onCreate by examining whether the Bundle is null or not. Since in @AfterViews there is no Bundle argument, I have an idea to support such that goal. The code below is just my idea, maybe you have a better idea (or not to accept the idea).

List and usages of the new annotation(s)

No new annotation, only allowing @AfterViews annotated method to have a boolean argument.

    @AfterViews
    void initViews(boolean firstlyCreated) {
        ...
    }

Examples of the generated code

    ...

    private boolean firstlyCreated_;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        firstlyCreated_ = savedInstanceState == null;
    }

    @Override
    public void onViewChanged(HasViews hasViews) {
        ...
        initViews(firstlyCreated_);
    }
@WonderCsabo
Copy link
Member

I agree, this is a valid request. However, i would pass the Bundle instance, not just a boolean. @dodgex WDYT?

@dodgex
Copy link
Member

dodgex commented Jan 31, 2017

While I see the goal - knowing if an Activity or Fragment is newly created or recreated - as a valid request. I'm not sure if @AfterViews is a good place? but as i don't know a better place i think it is okay.

I think I'd prefer to pass the Bundle too. but we have to ensure to null the reference after all @AfterViews methods have been called. (the sample is missing the change of the boolean)

@fikr4n
Copy link
Author

fikr4n commented Mar 6, 2017

Quoting @dodgex

I think I'd prefer to pass the Bundle too.

Today I found it's a great idea too. When using a library which needs to restore instance state by passing it savedInstanceState I think that I need the Bundle like you said, because I restore the library's component's views in @AfterViews void initViews().

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

No branches or pull requests

3 participants