资讯专栏INFORMATION COLUMN

不需要再手写 onSaveInstanceState 了,因为你的时间非常值钱

RichardXG / 329人阅读

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

如果你是一个有经验的 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> 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,谢谢 :)

SaveState Github 地址:https://github.com/PrototypeZ/SaveState

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/2712.html

相关文章

  • 需要手写 onSaveInstanceState 因为你的时间非常值钱

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

    ispring 评论0 收藏0
  • 刷到就是赚到!八月阿里 Android 高级岗面经(年薪百万)

    摘要:前段时间,前同事跳槽,机缘巧合下面了阿里,本来凭着试一试的态度,却不料好事成双,拿到了,而且薪资也了。面就没啥东西可聊的,基本上就是对此次面试的一个评价定薪等等一些之内的话题。如果是现场面试,记得关注当天的天气,提前查一下路线。 ...

    aisuhua 评论0 收藏0
  • 【回顾九月份第一周】 为什么你的前端工作经验值钱

    摘要:片中的受访者几乎涵盖了全部当时主流英伦摇滚的核心人物成员。一本书籍这周不宜读书老板,麻烦来一斤梦想,我可以可以在线支付吗查看更多列表回顾九月份第一周为什么你的前端工作经验不值钱回顾九月份第二周前端你该知道的事儿回顾九月份第三周最近的资讯集合 原链接:http://bluezhan.me/weekly/#/9-1 1、web前端 JavaScript 函数式编程术语大全 Segment...

    hlcc 评论0 收藏0
  • 【回顾九月份第一周】 为什么你的前端工作经验值钱

    摘要:片中的受访者几乎涵盖了全部当时主流英伦摇滚的核心人物成员。一本书籍这周不宜读书老板,麻烦来一斤梦想,我可以可以在线支付吗查看更多列表回顾九月份第一周为什么你的前端工作经验不值钱回顾九月份第二周前端你该知道的事儿回顾九月份第三周最近的资讯集合 原链接:http://bluezhan.me/weekly/#/9-1 1、web前端 JavaScript 函数式编程术语大全 Segment...

    Tony_Zby 评论0 收藏0
  • 【回顾九月份第一周】 为什么你的前端工作经验值钱

    摘要:片中的受访者几乎涵盖了全部当时主流英伦摇滚的核心人物成员。一本书籍这周不宜读书老板,麻烦来一斤梦想,我可以可以在线支付吗查看更多列表回顾九月份第一周为什么你的前端工作经验不值钱回顾九月份第二周前端你该知道的事儿回顾九月份第三周最近的资讯集合 原链接:http://bluezhan.me/weekly/#/9-1 1、web前端 JavaScript 函数式编程术语大全 Segment...

    JowayYoung 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<