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.

@InstanceState on custom view is misleading #1959

Description

@yolapop

AndroidAnnotations version: 4.2.0

Android compile SDK version: 25

Hi, i found an odd API here. When I found AA has supported @InstanceState, I was happy that I don't have to write custom parcelable again. But, it doesn't work like what I expected.

As you can see here I'm saving view states (background, checked state) on @InstanceState.

Annotated code:

@InstanceState protected boolean checked = false;
@InstanceState protected String label;
@InstanceState protected boolean selectableMode = false;
@InstanceState protected int normalBackgroundColor, selectedBackgroundColor;

But after onRestoreInstanceState is called, there is no way to set the restored states back to the view.

Generated code:

@Override
public void onRestoreInstanceState(Parcelable state) {
    Bundle bundle_ = ((Bundle) state);
    Parcelable instanceState = bundle_.getParcelable(INSTANCE_STATE_KEY);
    super.onRestoreInstanceState(instanceState);
    checked = bundle_.getBoolean("checked");
    label = bundle_.getString("label");
    selectableMode = bundle_.getBoolean("selectableMode");
    normalBackgroundColor = bundle_.getInt("normalBackgroundColor");
    selectedBackgroundColor = bundle_.getInt("selectedBackgroundColor");
}

Activity

  1. dodgex commented on Feb 27, 2017

    @dodgex
    Member

    But after onRestoreInstanceState is called, there is no way to set the restored states back to the view.

    Not sure what you mean? can you post some more code? in what class do you use @InstanceState and from what class is the generated code?

  2. yolapop commented on Feb 27, 2017

    @yolapop
    Author

    @dodgex

    in this case I want to re-check the item based on what is restored

    // generated
    @Override
    public void onRestoreInstanceState(Parcelable state) {
        Bundle bundle_ = ((Bundle) state);
        Parcelable instanceState = bundle_.getParcelable(INSTANCE_STATE_KEY);
        super.onRestoreInstanceState(instanceState);
        checked = bundle_.getBoolean("checked");
    }

    but the custom view is not notified after onRestoreInstanceState is called so I don't know how to re-check the view.

  3. dodgex commented on Feb 27, 2017

    @dodgex
    Member

    ah okay so your issue is that you get the values but you don't get a notifications when the state is set.

    looks like we need some kind of @AfterInstanceState or something similar. @WonderCsabo wdyt?

  4. dodgex commented on Feb 27, 2017

    @dodgex
    Member

    or we could just move the super.onRestoreInstanceState(instanceState); method to be called last. so that first the generated class restores its state and then we are calling into the parent.

  5. dodgex commented on Mar 7, 2017

    @dodgex
    Member

    I have openend a PR to move the super.onRestoreInstanceState(instanceState); call below the state restorations

  6. self-assigned this
    on Mar 7, 2017
  7. yolapop commented on Mar 20, 2017

    @yolapop
    Author

    thank you

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions