Giter VIP home page Giter VIP logo

savestate's Introduction

SaveState

Download

自动恢复 Activity、Fragment 以及 View 的状态。

无需任何类似 onSaveInstanceState 以及 onRestoreInstanceState 的模板代码。

如何使用

在成员变量上标记 @AutoRestore 注解。

在 Activity 中使用:

public class MyActivity extends Activity {

    @AutoRestore
    int myInt;

    @AutoRestore
    IBinder myRpcCall;

    @AutoRestore
    String result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Your code here
    }
}

在 Fragment 中使用:

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
    }
}

在自定义 View 中使用:

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

}

没错,就这么简单,只需要在变量上标记 @AutoRestore 注解。

现在就接入 SaveState

在项目根目录的 build.gradle 中增加以下内容:

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        // your other dependencies

        // dependency for save-state
        classpath "io.github.prototypez:save-state:${latest_version}"
    }
}

SaveState 当前最新版本: Download

然后在您的项目的 application 模块的 build.gradle 中应用插件:

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

如果你的项目中还有其他 library 模块也需要使用 SaveState,那么只需要在对应模块的 build.gradle 中也应用插件即可:

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

支持的变量类型

如果被 @AutoRestore 标记的字段是下列类型之一,那么不需要额外的配置, SaveState 就可以帮您自动保存与恢复变量。

基本数据类型(包装类型) 对象 数组
int/Integer Serializable byte[]
long/Long IBinder short[]
short/Short Bundle char[]
float/Float CharSequence float[]
double/Double Parcelable CharSequence[]
byte/Byte Size Parcelable[]
char/Character SizeF
boolean/Boolean String

自定义变量类型

如果您需要自动保存与恢复的变量不属于上面类型中的任意一种,并且您的变量类型可以被序列化为 JSON , 那么 SaveState 可以通过把这个对象和与它对应的 JSON 字符串之间的序列化和反序列化操作, 来实现变量的自动保存与恢复。

例如自定义对象:

public class User {
    String name;
    int age;
}


public class NetworkResponse<T> {
    int resultCode;

    T data
}

类似这样的对象都可以通过 SaveState 来自动恢复:

public class MyActivity extends Activity {

    @AutoRestore
    User user;

    @AutoRestore
    NetworkResponse<List<User>> response;
}

额外的配置操作有:

  1. 确保项目中已引入 支持的JSON序列化库 之一
  2. 在模块( com.android.application / com.android.library )的 build.gradle 中加入配置:
defaultConfig {

    // your other configs

    // 配置 JSON 序列化库
    javaCompileOptions {
        annotationProcessorOptions {
            arguments = [ serializer : "/*JSON库*/" ]
        }
    }
}

目前支持的 JSON 序列化库有:

LICENSE

Copyright (c) 2016-present, SateState Contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

savestate's People

Contributors

prototypez avatar

Watchers

HelloCsl avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.