Move super call onRestoreInstanceState below state restoration by dodgex · Pull Request #1968 · androidannotations/androidannotations · GitHub
Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright (C) 2016-2017 the AndroidAnnotations project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed To in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.androidannotations.test.instancestate;

import static org.fest.assertions.api.Assertions.assertThat;

import org.androidannotations.test.EmptyActivityWithoutLayout_;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;

import android.content.Context;
import android.os.Bundle;

@RunWith(RobolectricTestRunner.class)
public class InstanceStateViewTest {

@Test
public void testInstanceState() {
int restoredValue = 42;

Bundle bundle = new Bundle();
bundle.putInt("instanceField", restoredValue);

Context context = Robolectric.buildActivity(EmptyActivityWithoutLayout_.class).create().get();
InstanceStateView stateView = InstanceStateView_.build(context);

assertThat(stateView.instanceField).isEqualTo(-1);
assertThat(stateView.restoredInRestoreInstanceState).isEqualTo(-2);

stateView.onRestoreInstanceState(bundle);

assertThat(stateView.instanceField).isEqualTo(restoredValue);
assertThat(stateView.restoredInRestoreInstanceState).isEqualTo(restoredValue);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
* Copyright (C) 2016-2017 the AndroidAnnotations project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
Expand Down Expand Up @@ -468,6 +469,11 @@ public JMethod getRestoreStateMethod() {
return instanceStateDelegate.getRestoreStateMethod();
}

@Override
public JBlock getRestoreStateMethodBody() {
return instanceStateDelegate.getRestoreStateMethodBody();
}

@Override
public JVar getRestoreStateBundleParam() {
return instanceStateDelegate.getRestoreStateBundleParam();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ public JMethod getRestoreStateMethod() {
return instanceStateDelegate.getRestoreStateMethod();
}

@Override
public JBlock getRestoreStateMethodBody() {
return instanceStateDelegate.getRestoreStateMethodBody();
}

@Override
public JVar getRestoreStateBundleParam() {
return instanceStateDelegate.getRestoreStateBundleParam();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ public JMethod getRestoreStateMethod() {
return instanceStateDelegate.getRestoreStateMethod();
}

@Override
public JBlock getRestoreStateMethodBody() {
return instanceStateDelegate.getRestoreStateMethodBody();
}

@Override
public JVar getRestoreStateBundleParam() {
return instanceStateDelegate.getRestoreStateBundleParam();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
* Copyright (C) 2016-2017 the AndroidAnnotations project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
Expand All @@ -26,5 +27,7 @@ public interface HasInstanceState extends GeneratedClassHolder {

JMethod getRestoreStateMethod();

JBlock getRestoreStateMethodBody();

JVar getRestoreStateBundleParam();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
* Copyright (C) 2016-2017 the AndroidAnnotations project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
Expand Down Expand Up @@ -31,6 +32,7 @@ public class InstanceStateDelegate extends GeneratedClassHolderDelegate<ECompone
private JBlock saveStateMethodBody;
private JVar saveStateBundleParam;
private JMethod restoreStateMethod;
private JBlock restoreStateMethodBody;
private JVar restoreStateBundleParam;

public InstanceStateDelegate(EComponentHolder holder) {
Expand Down Expand Up @@ -71,6 +73,14 @@ public JMethod getRestoreStateMethod() {
return restoreStateMethod;
}

@Override
public JBlock getRestoreStateMethodBody() {
if (restoreStateMethodBody == null) {
setRestoreStateMethod();
}
return restoreStateMethodBody;
}

@Override
public JVar getRestoreStateBundleParam() {
if (restoreStateBundleParam == null) {
Expand All @@ -84,7 +94,8 @@ private void setRestoreStateMethod() {
restoreStateBundleParam = restoreStateMethod.param(getClasses().BUNDLE, "savedInstanceState");
holder.getInitBodyInjectionBlock().invoke(restoreStateMethod).arg(restoreStateBundleParam);

restoreStateMethod.body() //
restoreStateMethodBody = restoreStateMethod.body();
restoreStateMethodBody //
._if(ref("savedInstanceState").eq(_null())) //
._then()._return();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class ViewInstanceStateDelegate extends GeneratedClassHolderDelegate<ECom
private JBlock saveStateMethodBody;
private JVar saveStateBundleParam;
private JMethod restoreStateMethod;
private JBlock restoreStateMethodBody;
private JVar restoreStateBundleParam;

public ViewInstanceStateDelegate(EComponentHolder holder) {
Expand Down Expand Up @@ -84,6 +85,14 @@ public JMethod getRestoreStateMethod() {
return restoreStateMethod;
}

@Override
public JBlock getRestoreStateMethodBody() {
if (restoreStateMethodBody == null) {
setRestoreStateMethod();
}
return restoreStateMethodBody;
}

@Override
public JVar getRestoreStateBundleParam() {
if (restoreStateBundleParam == null) {
Expand All @@ -97,10 +106,13 @@ private void setRestoreStateMethod() {
restoreStateMethod.annotate(Override.class);
JVar state = restoreStateMethod.param(getClasses().PARCELABLE, "state");

JBlock restoreStateMethodBody = restoreStateMethod.body();
restoreStateBundleParam = restoreStateMethodBody.decl(getClasses().BUNDLE, "bundle" + generationSuffix(), cast(getClasses().BUNDLE, state));
JVar instanceState = restoreStateMethodBody.decl(getClasses().PARCELABLE, "instanceState", restoreStateBundleParam.invoke("getParcelable").arg(getInstanceStateKey()));
restoreStateMethodBody.invoke(_super(), "onRestoreInstanceState").arg(instanceState);
JBlock body = restoreStateMethod.body();
restoreStateBundleParam = body.decl(getClasses().BUNDLE, "bundle" + generationSuffix(), cast(getClasses().BUNDLE, state));
JVar instanceState = body.decl(getClasses().PARCELABLE, "instanceState", restoreStateBundleParam.invoke("getParcelable").arg(getInstanceStateKey()));

restoreStateMethodBody = body.blockSimple();

body.invoke(_super(), "onRestoreInstanceState").arg(instanceState);
}

private JVar getInstanceStateKey() {
Expand Down