Skip to content

Commit 9473c7f

Browse files
author
xiaoqi
committed
添加对支持kotlin
1 parent 95d9097 commit 9473c7f

6 files changed

Lines changed: 124 additions & 16 deletions

File tree

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@
3030
implementation 'com.noober:savehelper-api:2.1.0'
3131
annotationProcessor 'com.noober:processor:2.1.0'
3232

33+
kotlin的依赖方式
34+
35+
apply plugin: 'kotlin-kapt'
36+
apply plugin: 'kotlin-android-extensions'
37+
apply plugin: 'kotlin-android'
38+
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'
42+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
43+
3344
混淆配置:
3445

3546
-dontwarn com.noober.**
@@ -245,10 +256,54 @@ savedInstanceState不会null的时候,说明就是需要内存恢复的时候
245256
}
246257
}
247258

259+
# kotlin使用方法
260+
261+
如果要在kotlin使用,与在java中使用相同,直接加注解即可,但是不同之出在于:
262+
263+
1:如果是基本数据类型,需要多添加一个注解@JvmField
264+
265+
2:如果是其他数据类型,需要增加lateinit关键字或者添加一个注解@JvmField
266+
否则会报错"the modifier of the field must not be private, otherwise it won't work"。
267+
268+
示例:
269+
270+
271+
class KotlinActivity : AppCompatActivity() {
272+
273+
@NeedSave
274+
@JvmField
275+
var a :Int=3
276+
277+
@NeedSave
278+
lateinit var bundle: Bundle
279+
280+
override fun onCreate(savedInstanceState: Bundle?) {
281+
super.onCreate(savedInstanceState)
282+
setContentView(R.layout.activity_kotlin)
283+
SaveHelper.recover(this, savedInstanceState)
284+
Log.e("KotlinActivity", a.toString())
285+
286+
}
287+
288+
289+
override fun onSaveInstanceState(outState: Bundle?) {
290+
Log.e("KotlinActivity", "onSaveInstanceState")
291+
a = 2
292+
SaveHelper.save(this, outState)
293+
super.onSaveInstanceState(outState)
294+
}
295+
}
296+
297+
248298
# 总结
249299
看到这里大家已经猜到其实这个框架的实现原理和ButterKnife是相同的。而bufferknife的原理很多文章都有,这里就不过多介绍了。
250300

251301
# 更新
302+
303+
304+
## 2.0.0
305+
支持Bundle所有支持的的类型
306+
252307
## 2.1.0
253308
增加对PersistableBundle持久化数据的保存,用于手机关机重启后的数据恢复,使用方法如下:
254309

build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

3-
buildscript {
3+
buildscript {
4+
ext.kotlin_version = '1.2.30'
5+
46
repositories {
57
jcenter()
68
maven {
@@ -13,7 +15,7 @@ buildscript {
1315
classpath 'com.novoda:bintray-release:0.5.0'
1416
// NOTE: Do not place your application dependencies here; they belong
1517
// in the individual module build.gradle files
16-
}
18+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }
1719
}
1820

1921
allprojects {

sample/build.gradle

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-kapt'
3+
apply plugin: 'kotlin-android-extensions'
4+
apply plugin: 'kotlin-android'
25

36
android {
47
compileSdkVersion 26
@@ -29,10 +32,13 @@ dependencies {
2932
})
3033
implementation 'com.android.support:appcompat-v7:26.0.1'
3134
testImplementation 'junit:junit:4.12'
32-
// implementation 'com.noober:savehelper:2.1.0'
33-
// annotationProcessor 'com.noober:processor:2.1.0'
34-
// implementation 'com.noober:savehelper-api:2.1.0'
35-
implementation project(':savehelper')
36-
implementation project(':savehelper-api')
37-
annotationProcessor project(':savehelper-processor')
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'
38+
// implementation project(':savehelper')
39+
// implementation project(':savehelper-api')
40+
// kapt project(':savehelper-processor')
41+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
3842
}
43+
44+
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.recover.autosavesample">
3+
package="com.recover.autosavesample">
44

55
<application
66
android:allowBackup="true"
77
android:label="@string/app_name"
8-
android:theme="@style/Theme.AppCompat.NoActionBar"
9-
android:supportsRtl="true">
10-
<activity android:name=".SampleActivity"
8+
android:supportsRtl="true"
9+
android:theme="@style/Theme.AppCompat.NoActionBar">
10+
<activity
11+
android:name=".SampleActivity"
1112
android:persistableMode="persistAcrossReboots">
13+
14+
</activity>
15+
<activity android:name=".TestActivity"></activity>
16+
<activity android:name=".KotlinActivity">
1217
<intent-filter>
13-
<action android:name="android.intent.action.MAIN"/>
18+
<action android:name="android.intent.action.MAIN" />
1419

15-
<category android:name="android.intent.category.LAUNCHER"/>
20+
<category android:name="android.intent.category.LAUNCHER" />
1621
</intent-filter>
1722
</activity>
18-
<activity android:name=".TestActivity">
19-
</activity>
2023
</application>
2124

2225
</manifest>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.recover.autosavesample
2+
3+
import android.support.v7.app.AppCompatActivity
4+
import android.os.Bundle
5+
import android.util.Log
6+
import com.noober.api.NeedSave
7+
import com.noober.savehelper.SaveHelper
8+
9+
class KotlinActivity : AppCompatActivity() {
10+
11+
@NeedSave
12+
@JvmField
13+
var a :Int=3
14+
15+
@NeedSave
16+
lateinit var bundle: Bundle
17+
18+
override fun onCreate(savedInstanceState: Bundle?) {
19+
super.onCreate(savedInstanceState)
20+
setContentView(R.layout.activity_kotlin)
21+
SaveHelper.recover(this, savedInstanceState)
22+
Log.e("KotlinActivity", a.toString())
23+
24+
}
25+
26+
27+
override fun onSaveInstanceState(outState: Bundle?) {
28+
Log.e("KotlinActivity", "onSaveInstanceState")
29+
a = 2
30+
SaveHelper.save(this, outState)
31+
super.onSaveInstanceState(outState)
32+
}
33+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".KotlinActivity">
8+
9+
</android.support.constraint.ConstraintLayout>

0 commit comments

Comments
 (0)