Skip to content

Commit b2fda37

Browse files
author
xiaoqi
committed
update to 2.2.6
1 parent 9473c7f commit b2fda37

File tree

13 files changed

+92
-64
lines changed

13 files changed

+92
-64
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@
2121
2.0.4 修复枚举类型保存的时候不能识别的问题
2222

2323
2.1.0 增加对PersistableBundle的支持,NeedSave注解中设置isPersistable = true则说明该参数保存到PersistableBundle
24-
24+
2.2.6 增加对自定义view的数据保存以及恢复
2525

2626
引入方式,在app的gradle中加入下面依赖即可:
2727

2828

29-
implementation 'com.noober:savehelper:2.1.0'
30-
implementation 'com.noober:savehelper-api:2.1.0'
31-
annotationProcessor 'com.noober:processor:2.1.0'
29+
implementation 'com.noober:savehelper:2.2.6'
30+
implementation 'com.noober:savehelper-api:2.2.6'
31+
annotationProcessor 'com.noober:processor:2.2.6'
3232

3333
kotlin的依赖方式
3434

3535
apply plugin: 'kotlin-kapt'
3636
apply plugin: 'kotlin-android-extensions'
3737
apply plugin: 'kotlin-android'
3838

39-
implementation 'com.noober:savehelper:2.1.0'
40-
kapt 'com.noober:processor:2.1.0'
41-
implementation 'com.noober:savehelper-api:2.1.0'
39+
implementation 'com.noober:savehelper:2.2.6'
40+
kapt 'com.noober:processor:2.2.6'
41+
implementation 'com.noober:savehelper-api:2.2.6'
4242
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
4343

4444
混淆配置:
@@ -140,6 +140,43 @@ android 内存被回收是一个开发者的常见问题。当我们**跳转到
140140

141141
这样就不会因为这种太多的重复的操作去导致代码逻辑的混乱,同时也避免了敲代码时因为key写错导致的错误。
142142

143+
自定义view的变量保存示例:
144+
145+
public class CustomView extends View {
146+
147+
@NeedSave
148+
int a;
149+
150+
public CustomView(Context context) {
151+
super(context);
152+
}
153+
154+
public CustomView(Context context, @Nullable AttributeSet attrs) {
155+
super(context, attrs);
156+
}
157+
158+
159+
public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
160+
super(context, attrs, defStyleAttr);
161+
}
162+
163+
164+
@Override
165+
protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
166+
SaveHelper.save(this, container);
167+
super.dispatchSaveInstanceState(container);
168+
}
169+
170+
@Override
171+
protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
172+
super.dispatchRestoreInstanceState(container);
173+
SaveHelper.recover(this, container);
174+
}
175+
}
176+
177+
178+
重写dispatchSaveInstanceState,dispatchRestoreInstanceState即可。
179+
注意SaveHelper.save(this, container)要在super.dispatchSaveInstanceState(container);**之前调用**
143180
# 效果展示
144181
我们来看一下测试代码:
145182
## 不进行数据保存操作

build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ allprojects {
2626
name 'Google'
2727
}
2828
}
29+
tasks.withType(Javadoc) {
30+
options.addStringOption('Xdoclint:none', '-quiet')
31+
options.addStringOption('encoding', 'UTF-8')
32+
}
2933
}
3034

3135
task clean(type: Delete) {
@@ -37,7 +41,7 @@ ext {
3741
userOrg = 'noober'
3842
groupId = 'com.noober'
3943
uploadName = 'AutoSaver'
40-
publishVersion = '2.1.1'
44+
publishVersion = '2.2.6'
4145
desc = "A light weight framework can automatically generate 'OnSaveInstanceState' code"
4246
website = 'https://github.com/JavaNoober/AutoSave'
4347
// gradlew clean build bintrayUpload -PbintrayUser=xiaoqiandroid -PbintrayKey=xxxxxxxxxxxxxxxx -PdryRun=false

gradle.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
# Specifies the JVM arguments used for the daemon process.
1111
# The setting is particularly useful for tweaking memory settings.
1212
#org.gradle.jvmargs=-Xmx1536m
13-
org.gradle.jvmargs= -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
13+
#org.gradle.jvmargs= -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005;
14+
org.gradle.jvmargs=-Xmx512m
1415
org.gradle.parallel=true
1516
# When configured, Gradle will run in incubating parallel mode.
1617
# This option should only be used with decoupled projects. More details, visit

sample/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ dependencies {
3131
exclude group: 'com.android.support', module: 'support-annotations'
3232
})
3333
implementation 'com.android.support:appcompat-v7:26.0.1'
34-
testImplementation 'junit:junit:4.12'
35-
implementation 'com.noober:savehelper:2.1.0'
36-
kapt 'com.noober:processor:2.1.0'
37-
implementation 'com.noober:savehelper-api:2.1.0'
34+
// testImplementation 'junit:junit:4.12'
35+
implementation 'com.noober:savehelper:2.2.6'
36+
kapt 'com.noober:processor:2.2.6'
37+
implementation 'com.noober:savehelper-api:2.2.6'
3838
// implementation project(':savehelper')
3939
// implementation project(':savehelper-api')
4040
// kapt project(':savehelper-processor')

sample/src/main/AndroidManifest.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
<activity
1111
android:name=".SampleActivity"
1212
android:persistableMode="persistAcrossReboots">
13-
14-
</activity>
15-
<activity android:name=".TestActivity"></activity>
16-
<activity android:name=".KotlinActivity">
1713
<intent-filter>
1814
<action android:name="android.intent.action.MAIN" />
1915

2016
<category android:name="android.intent.category.LAUNCHER" />
2117
</intent-filter>
2218
</activity>
19+
<activity android:name=".TestActivity"></activity>
20+
<activity android:name=".KotlinActivity">
21+
22+
</activity>
2323
</application>
2424

2525
</manifest>

sample/src/main/java/com/recover/autosavesample/CustomView.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,15 @@ public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAtt
3434
}
3535

3636

37-
@Nullable
38-
@Override
39-
protected Parcelable onSaveInstanceState() {
40-
Log.e("CustomView", "onSaveInstanceState");
41-
return super.onSaveInstanceState();
42-
}
43-
4437
@Override
4538
protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
46-
4739
SaveHelper.save(this, container);
48-
a = 2;
49-
50-
Bundle bundle = new Bundle();
51-
bundle.putInt("A", a);
52-
container.put(1, bundle);
5340
super.dispatchSaveInstanceState(container);
54-
Log.e("CustomView", "dispatchSaveInstanceState");
55-
}
56-
57-
@Override
58-
protected void onRestoreInstanceState(Parcelable state) {
59-
super.onRestoreInstanceState(state);
60-
Log.e("CustomView", "onRestoreInstanceState");
6141
}
6242

6343
@Override
6444
protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
6545
super.dispatchRestoreInstanceState(container);
6646
SaveHelper.recover(this, container);
67-
Bundle bundle = (Bundle) container.get(1);
68-
a = bundle.getInt("A");
69-
Log.e("CustomView", "dispatchRestoreInstanceState");
7047
}
7148
}

sample/src/main/java/com/recover/autosavesample/SampleActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ public class SampleActivity extends AppCompatActivity {
7878
// SecondSExample2 secondSExample2;
7979

8080

81+
// CustomView customView;
8182

8283
@Override
8384
protected void onCreate(Bundle savedInstanceState) {
8485
super.onCreate(savedInstanceState);
8586
setContentView(R.layout.activity_main);
87+
// customView = findViewById(R.id.view);
8688
initData();
8789
Log.e("Sample", i + "");
8890
if(savedInstanceState != null){
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout
3-
xmlns:android="http://schemas.android.com/apk/res/android"
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
43
xmlns:tools="http://schemas.android.com/tools"
54
android:id="@+id/activity_main"
65
android:layout_width="match_parent"
@@ -10,8 +9,9 @@
109
android:paddingRight="@dimen/activity_horizontal_margin"
1110
android:paddingTop="@dimen/activity_vertical_margin"
1211
tools:context="com.recover.autosavesample.SampleActivity">
13-
<com.recover.autosavesample.CustomView
14-
android:id="@+id/view"
15-
android:layout_width="match_parent"
16-
android:layout_height="match_parent" />
12+
13+
<com.recover.autosavesample.CustomView
14+
android:id="@+id/view"
15+
android:layout_width="match_parent"
16+
android:layout_height="match_parent" />
1717
</RelativeLayout>

savehelper-processor/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ apply plugin: 'bintray-release'
44
dependencies {
55
implementation fileTree(include: ['*.jar'], dir: 'libs')
66
implementation 'com.squareup:javapoet:1.8.0'
7-
compile project(':savehelper-api')
7+
implementation project(':savehelper-api')
88
}
99

1010
sourceCompatibility = "1.7"

savehelper/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22
apply plugin: 'bintray-release'
33

44
android {
5-
compileSdkVersion 26
5+
compileSdkVersion 27
66
buildToolsVersion '26.0.2'
77

88
defaultConfig {
@@ -28,10 +28,10 @@ android {
2828

2929
dependencies {
3030
implementation fileTree(include: ['*.jar'], dir: 'libs')
31-
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
31+
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
3232
exclude group: 'com.android.support', module: 'support-annotations'
3333
})
34-
// provided 'com.android.support:appcompat-v7:+'
34+
// provided 'com.android.support:appcompat-v7:+'
3535
testImplementation 'junit:junit:4.12'
3636
}
3737

savehelper/src/main/java/com/noober/savehelper/SaveHelper.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class SaveHelper {
1515

1616
private final static String HELPER_END = "_SaveStateHelper";
1717

18+
private final static int VIEW_STATE_KEY = -999;
19+
1820
/**
1921
* equate to{@link SaveHelper#recover}, it's just renamed to make it easier to understand
2022
*/
@@ -24,7 +26,7 @@ public static <T> void bind(T recover, Bundle savedInstanceState){
2426
}
2527

2628
/**
27-
* added while need to recover data, used in {@link android.app.Activity#onCreate(Bundle)}
29+
* added while need to recover data, used in {android.app.Activity#onCreate(Bundle)}
2830
*
2931
* @param recover current Activity or Fragment
3032
* @param savedInstanceState Bundle
@@ -34,7 +36,7 @@ public static <T> void recover(T recover, Bundle savedInstanceState){
3436
}
3537

3638
/**
37-
* equate to{@link SaveHelper#recover(Object, Bundle)}, used in {@link android.app.Activity#onCreate(Bundle, PersistableBundle)}
39+
* equate to{@link SaveHelper#recover(Object, Bundle)}, used in {android.app.Activity#onCreate(Bundle, PersistableBundle)}
3840
* added in 2.1.0
3941
*
4042
* @param recover current Activity or Fragment
@@ -50,7 +52,7 @@ public static <T> void recover(T recover, Bundle savedInstanceState, Persistable
5052
}
5153

5254
/**
53-
* added while need to save data, used in {@link android.app.Activity#onSaveInstanceState(Bundle)}
55+
* added while need to save data, used in {android.app.Activity#onSaveInstanceState(Bundle)}
5456
*
5557
* @param save current Activity or Fragment
5658
* @param outState Bundle
@@ -60,7 +62,7 @@ public static <T> void save(T save, Bundle outState){
6062
}
6163

6264
/**
63-
* equate to{@link SaveHelper#save(Object, Bundle)}, used in {@link android.app.Activity#onSaveInstanceState(Bundle, PersistableBundle)}
65+
* equate to{@link SaveHelper#save(Object, Bundle)}, used in {android.app.Activity#onSaveInstanceState(Bundle, PersistableBundle)}
6466
* added in 2.1.0
6567
*
6668
* @param save current Activity or Fragment
@@ -84,9 +86,14 @@ public static <T> void save(T save, Bundle outState, PersistableBundle persisten
8486
*/
8587
public static <T> void save(T save, SparseArray<Parcelable> container){
8688
if(container != null){
87-
ISaveViewStateHelper<T> viewSaveHelper = findViewSaveHelper(save);
88-
if(viewSaveHelper != null){
89-
viewSaveHelper.save(save, container);
89+
ISaveInstanceStateHelper<T> viewStateHelper = findSaveHelper(save);
90+
if(viewStateHelper != null){
91+
Bundle bundle = (Bundle) container.get(VIEW_STATE_KEY);
92+
if(bundle == null){
93+
bundle = new Bundle();
94+
}
95+
viewStateHelper.save(bundle, null, save);
96+
container.put(VIEW_STATE_KEY, bundle);
9097
}
9198
}
9299
}
@@ -99,9 +106,12 @@ public static <T> void save(T save, SparseArray<Parcelable> container){
99106
*/
100107
public static <T> void recover(T save, SparseArray<Parcelable> container){
101108
if(container != null){
102-
ISaveViewStateHelper<T> viewSaveHelper = findViewSaveHelper(save);
103-
if(viewSaveHelper != null){
104-
viewSaveHelper.recover(save, container);
109+
ISaveInstanceStateHelper<T> viewStateHelper = findSaveHelper(save);
110+
if(viewStateHelper != null){
111+
Bundle bundle = (Bundle) container.get(VIEW_STATE_KEY);
112+
if(bundle != null){
113+
viewStateHelper.recover(bundle, null, save);
114+
}
105115
}
106116
}
107117
}

0 commit comments

Comments
 (0)