若是你是一个有经验的 Android 程序员,那么你确定手写过许多 onSaveInstanceState
以及 onRestoreInstanceState
方法用来保持 Activity 的状态,由于 Activity 在变为不可见之后,系统随时可能把它回收用来释放内存。重写 Activity 中的 onSaveInstanceState
方法 是 Google 推荐的用来保持 Activity 状态的作法。java
onSaveInstanceState
方法会提供给咱们一个 Bundle
对象用来保存咱们想保存的值,可是 Bundle
存储是基于 key - value 这样一个形式,因此咱们须要定义一些额外的 String
类型的 key 常量,最后咱们的项目中会充斥着这样代码:android
static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
// ...
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
复制代码
保存完状态以后,为了能在系统从新实例化这个 Activity 的时候恢复先前被系统杀死前的状态,咱们在 onCreate
方法里把原来保存的值从新取出来:git
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Always call the superclass first
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
} else {
// Probably initialize members with default values for a new instance
}
// ...
}
复制代码
固然,恢复这个操做也能够在 onRestoreInstanceState
这个方法实现:程序员
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);
// Restore state members from saved instance
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}
复制代码
上面的方案固然是正确的。可是并不优雅,为了保持变量的值,引入了两个方法 ( onSaveInstanceState
和 onRestoreInstanceState
) 和两个常量 ( 为了存储两个变量而定义的两个常量,仅仅为了放到 Bundle
里面)。github
为了更好地解决这个问题,我写了 SaveState 这个插件:app
在使用了 SaveState 这个插件之后,保持 Activity 的状态的写法以下:ide
public class MyActivity extends Activity {
@AutoRestore
int myInt;
@AutoRestore
IBinder myRpcCall;
@AutoRestore
String result;
@Override
protected void onCreate(Bundle savedInstanceState) {
// Your code here
}
}
复制代码
没错,你只须要在须要保持的变量上标记 @AutoRestore
注解便可,无需去管那几个烦人的 Activity 回调,也不须要定义多余的 String
类型 key 常量。gradle
那么,除了 Activity 之外,Fragment 能自动保持状态吗?答案是: Yes!ui
public class MyFragment extends Fragment {
@AutoRestore
User currentLoginUser;
@AutoRestore
List<Map<String, Object>> networkResponse;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// Your code here
}
}
复制代码
使用方法和 Activity 如出一辙!不止如此,使用场景还能够推广到 View
, 今后,你的自定义 View,也能够把状态保持这个任务交给 SaveState :google
public class MyView extends View {
@AutoRestore
String someText;
@AutoRestore
Size size;
@AutoRestore
float[] myFloatArray;
public MyView(Context context) {
super(context);
}
public MyView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
复制代码
引入 SaveState 的方法也十分简单:
首先,在项目根目录的 build.gradle
文件中增长如下内容:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
// your other dependencies
// dependency for save-state
classpath "io.github.prototypez:save-state:${latest_version}"
}
}
复制代码
而后,在 application 和 library 模块的 build.gradle
文件中应用插件:
apply plugin: 'com.android.application'
// apply plugin: 'com.android.library'
apply plugin: 'save.state'
复制代码
万事具有!不再须要写烦人的回调,由于你的时间很是值钱!作了一点微小的工做,若是我帮你节省下来了喝一杯咖啡的时间,但愿你能够帮我点一个 Star,谢谢 :)
SaveState Github 地址:github.com/PrototypeZ/…
Update: 最新版本已支持 Kotlin,Happy Hacking!