如果你是一个有经验的 Android 程序员,那么你肯定手写过许多 onSaveInstanceState 以及 onRestoreInstanceState 方法用来保持 Activity 的状态,因为 Activity 在变为不可见以后,系统随时可能把它回收用来释放内存。重写 Activity 中的 onSaveInstanceState 方法 是 Google 推荐的用来保持 Activity 状态的做法。

Google 推荐的最佳实践

onSaveInstanceState 方法会提供给我们一个 Bundle 对象用来保存我们想保存的值,但是 Bundle 存储是基于 key - value 这样一个形式,所以我们需要定义一些额外的 String 类型的 key 常量,最后我们的项目中会充斥着这样代码:

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 方法里把原来保存的值重新取出来:

@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);
}

解放你的双手

上面的方案当然是正确的。但是并不优雅,为了保持变量的值,引入了两个方法 ( onSaveInstanceStateonRestoreInstanceState ) 和两个常量 ( 为了存储两个变量而定义的两个常量,仅仅为了放到 Bundle 里面)。

为了更好地解决这个问题,我写了 SaveState 这个插件:

在使用了 SaveState 这个插件以后,保持 Activity 的状态的写法如下:

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 常量。

那么,除了 Activity 以外,Fragment 能自动保持状态吗?答案是: Yes!

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

public class MyView extends View {

    @AutoRestore
String someText; @AutoRestore
Size size; @AutoRestore
float[] myFloatArray; public MainView(Context context) {
super(context);
} public MainView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
} public MainView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
} }

现在就使用 SaveState

引入 SaveState 的方法也十分简单:

首先,在项目根目录的 build.gradle 文件中增加以下内容:

buildscript {

    repositories {
google()
jcenter()
}
dependencies {
// your other dependencies // dependency for save-state
classpath "io.github.prototypez:save-state:${latest_version}"
}
}

然后,在 applicationlibrary 模块的 build.gradle 文件中应用插件:

apply plugin: 'com.android.application'
// apply plugin: 'com.android.library'
apply plugin: 'save.state'

万事具备!再也不需要写烦人的回调,因为你的时间非常值钱!做了一点微小的工作,如果我帮你节省下来了喝一杯咖啡的时间,希望你可以帮我点一个 Star,谢谢

不需要再手写 onSaveInstanceState 了,因为你的时间非常值钱的更多相关文章

  1. 手写 Vue 系列 之 Vue1.x

    前言 前面我们用 12 篇文章详细讲解了 Vue2 的框架源码.接下来我们就开始手写 Vue 系列,写一个自己的 Vue 框架,用最简单的代码实现 Vue 的核心功能,进一步理解 Vue 核心原理. ...

  2. 一套手写ajax加一般处理程序的增删查改

    倾述下感受:8天16次驳回.这个惨不忍睹. 好了不说了,说多了都是泪. 直接上代码 : 这个里面的字段我是用动软生成的,感觉自己手写哪些字段太浪费时间了,说多了都是泪 ajax.model层的代码: ...

  3. 手写MVC框架(一)-再出发

    背景 前段时间把之前写的DAO框架(手写DAO框架(一)-从“1”开始)整理了一下,重构了一版.整理过程中看以前写的代码,只是为了了解实现,只是为了实现,代码写的有点粗糙.既然已经整理了DAO框架,索 ...

  4. Android 手写Binder 教你理解android中的进程间通信

    关于Binder,我就不解释的太多了,网上一搜资料一堆,但是估计还是很多人理解的有困难.今天就教你如何从 app层面来理解好Binder. 其实就从我们普通app开发者的角度来看,仅仅对于androi ...

  5. 【Xamarin挖墙脚系列:代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧(转)】

    正愁如何选择构建项目中的视图呢,现在官方推荐画板 Storybord...但是好像 xib貌似更胜一筹.以前的老棒子总喜欢装吊,用代码写....用代码堆一个HTML页面不知道你们尝试过没有.等页面做出 ...

  6. .netER的未来路,关于基础是否重要和应该自己手写代码吗?

    http://www.cnblogs.com/onepiece_wang/p/5558341.html#!comments 引用"基础知识的学习,一开始可能是背书,但是在后续若干年的工作过程 ...

  7. 关于代码手写UI,xib和StoryBoard

    代码手写UI 这种方法经常被学院派的极客或者依赖多人合作的大型项目大规模使用.Geek们喜欢用代码构建UI,是因为代码是键盘敲出来的,这样可以做到不开IB,手不离开键盘就完成工作,可以专注于编码环境, ...

  8. 手写@koa/router源码

    上一篇文章我们讲了Koa的基本架构,可以看到Koa的基本架构只有中间件内核,并没有其他功能,路由功能也没有.要实现路由功能我们必须引入第三方中间件,本文要讲的路由中间件是@koa/router,这个中 ...

  9. 手写koa-static源码,深入理解静态服务器原理

    这篇文章继续前面的Koa源码系列,这个系列已经有两篇文章了: 第一篇讲解了Koa的核心架构和源码:手写Koa.js源码 第二篇讲解了@koa/router的架构和源码:手写@koa/router源码 ...

随机推荐

  1. Java核心技术及面试指南 线性表方面的面试题总结以及答案

    3.2.7.1 请用ArrayList实现Stack以及Queue的功能. public class ArrayListStack extends ArrayList implements Stack ...

  2. mysql 开发进阶篇系列 16 MySQL Server(myisam key_buffer)

    一.概述 mysql 提供了很多参数来进行服务器的设置,当服务第一次启动的时候,所有启动参数值都是系统默认的.这些参数在很多生产环境下并不能满足实际的应用需求.在这个系列中涉及到了liunx 服务器, ...

  3. 开发属于自己的Web服务器

    本文基于.net core 的控制台程序作为服务端 main函数: class Program { static void Main(string[] args) { Console.WriteLin ...

  4. Salesforce Sales Cloud 零基础学习(四) Chatter

    Chatter是一个Salesforce实时协作应用程序,它允许你的用户一起工作.互相交谈和共享信息,不管用户角色或位置如何,连接.并激励用户在整个组织内高效工作. Chatter 让用户们在 Opp ...

  5. ajax实现文档导出及下载

    做导出一直遇到个问题就是不能用ajax实现一步导出文档,即导出加下载.今天突然想到可以分开来做就上网搜了下,发现一篇比较不错的文章(http://www.cnblogs.com/zj0208/p/59 ...

  6. 逆向实战干货,植物大战僵尸快速定位自动捡阳光Call,或者标志

    逆向实战干货,快速定位自动捡阳光Call,或者标志 注意: 关于CE和OD的使用,这里不再多说,快速定位,默认大家已经有了CE基础,或者OD基础. 第一种方法,找Call 第一步,打开CE,搜索阳光值 ...

  7. systemctl enable docker.service

    [root@dingyingsi ~]# systemctl start docker.service [root@dingyingsi ~]# systemctl enable docker.ser ...

  8. eclipse如何将项目上传到码云

    把Eclipse项目上传到码云的步骤: 一.将代码提交到本地 1.登录码云:新建项目 2.输入项目名: 3.空项目创建成功如下图: 4.右键点击Eclipse的项目,选择“Team”——>“Sh ...

  9. 【转】JQuery上传插件Uploadify使用详解及错误处理

    转自:http://www.jb51.net/article/43498.htm 关于JQuery上传插件Uploadify使用详解网上一大把,基本上内容都一样.我根据网上的步骤配置完成后,会报一些错 ...

  10. 第三讲 smart qq 登录成功后获取 vfwebqq ,psessionid,hash

    public static void Login_GetPHV() { string urldata = "{\"ptwebqq\":\"#{ptwebqq}\ ...