我用一个片段尝试了一些非常简单的测试。但是Icepick似乎并没有将String测试的值保存在outState捆绑包中。。。
public class MyFragment extends Fragment {
@Icicle String test;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Icepick.restoreInstanceState(this,savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_settings, container, false);
Log.d(TAG,"restored value of test="+test);
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
test="I have a value";
super.onSaveInstanceState(outState);
Icepick.saveInstanceState(this,outState);
Log.d(TAG, "test="+test);
}
我的等级结构如下:
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.my31daychallenge.mysleepchallenge"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
compile 'com.jakewharton.threetenabp:threetenabp:1.0.1'
apt 'com.bluelinelabs:logansquare-compiler:1.1.0'
compile 'com.bluelinelabs:logansquare:1.1.0'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'frankiesardo:icepick:3.0.0'
provided 'frankiesardo:icepick-processor:3.0.0'
}
注意,我尝试了Icepick 3.0.2和3.0.3-SNAPSHOT的最新版本,但都不起作用,所以我想使用早期版本,得到相同的结果。
以及:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven {url "https://clojars.org/repo/"}
}
}
我错过了什么?
在build.gradle文件中,对于此行provided 'frankiesardo:icepick-processor:3.0.0'
,将provided
更改为apt
。这就行了。
来源:https://github.com/frankiesardo/icepick/issues/56
好吧,我5分钟前就开始查看IcePick库了,所以如果这是一个愚蠢的答案,请记住:(
截至本承诺https://github.com/frankiesardo/icepick/commit/f60b4b9c0307be208178381661ee13b958eda531看起来应该使用@State而不是@Icicle。这能解决你的问题吗?
放入
Icepick.restoreInstanceState(this, savedInstanceState);
在onCreateView()
中(而不是在onCreate
中(