Skip to content

Commit 46f93b4

Browse files
committed
Fix @InstanceState calling order
In some scenarios, state restoration was called after @AfterInject methods are called. This commit fixes this issue by moving the restore call to the dedicated injection block.
1 parent ef9be30 commit 46f93b4

3 files changed

Lines changed: 78 additions & 5 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.instancestate;
17+
18+
import org.androidannotations.annotations.AfterInject;
19+
import org.androidannotations.annotations.EActivity;
20+
import org.androidannotations.annotations.InstanceState;
21+
22+
import android.app.Activity;
23+
24+
@EActivity
25+
public class InstanceStateAfterInjectActivity extends Activity {
26+
27+
@InstanceState
28+
int instanceField = -1;
29+
30+
int restoredInAfterInject;
31+
32+
@AfterInject
33+
void afterInject() {
34+
restoredInAfterInject = instanceField;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.instancestate;
17+
18+
import static org.fest.assertions.api.Assertions.assertThat;
19+
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.robolectric.Robolectric;
23+
import org.robolectric.RobolectricTestRunner;
24+
25+
import android.os.Bundle;
26+
27+
@RunWith(RobolectricTestRunner.class)
28+
public class InstanceStateAfterInjectActivityTest {
29+
30+
@Test
31+
public void testInstanceStateAfterInjectCallOrder() {
32+
int restoredValue = 42;
33+
34+
Bundle bundle = new Bundle();
35+
bundle.putInt("instanceField", restoredValue);
36+
37+
InstanceStateAfterInjectActivity_ activity = Robolectric.buildActivity(InstanceStateAfterInjectActivity_.class).create(bundle).get();
38+
39+
assertThat(activity.restoredInAfterInject).isEqualTo(restoredValue);
40+
}
41+
}

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/InstanceStateDelegate.java‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,10 @@ public JVar getRestoreStateBundleParam() {
8282
private void setRestoreStateMethod() {
8383
restoreStateMethod = getGeneratedClass().method(PRIVATE, codeModel().VOID, "restoreSavedInstanceState" + generationSuffix());
8484
restoreStateBundleParam = restoreStateMethod.param(getClasses().BUNDLE, "savedInstanceState");
85-
getInit().body().invoke(restoreStateMethod).arg(restoreStateBundleParam);
85+
holder.getInitBodyInjectionBlock().invoke(restoreStateMethod).arg(restoreStateBundleParam);
8686

8787
restoreStateMethod.body() //
8888
._if(ref("savedInstanceState").eq(_null())) //
8989
._then()._return();
9090
}
91-
92-
public JMethod getInit() {
93-
return holder.getInit();
94-
}
9591
}

0 commit comments

Comments
 (0)