AutoSave

Project Url: JavaNoober/AutoSave
Introduction: A framework can automatically generate OnSaveInstanceState code
More: Author   ReportBugs   
Tags:

license JCenter

该框架可以自动生成 OnSaveInstanceState 代码,保持内存恢复,支持 kotlin 使用

版本更新说明:

1.0.0 完成基本功能;
1.0.1 全局变量的作用域从之前强制 public 改成只要非 private 即可;
1.0.2 修改 SaveHelper.bind(this, savedInstanceState)方法为 SaveHelper.recover(this, savedInstanceState),只是重命名,
      以便于理解;
      去掉当内存被回收去调用 recover 方法时,却没有对应 helper 类会主动抛异常的情况,方便在 BaseAcitviy 和 BaseFragment 的
      onSaveInstanceState 和 onRestoreInstanceState 统一添加 SaveHelper.save 和 SaveHelper.recover 方法。
1.0.3 优化代码生成,如果一个 activity 或者 fragment 中没有有效的@NeedSave 注解,但是添加了 SaveHelper.recover 和 SaveHelper.save
      方法,现在就不会自动生成这个类的 SaveStateHelper 类,减少了无用 SaveStateHelper 类,便于在 Base 类中统一集成。

2.0.0 去掉 NeedSave 注解中的 isParcelable 字段,自动可以支持不同类型;
      如果字段被标记为 private 在编译的时候会抛异常;
      支持基本所有 bundle 可以传入的类型,包括 SparseParcelableArray 等, 如果传入的类型 bundle 不支持会抛异常(如果有遗漏的类型,请在 github 提出 issue);
2.0.2 修复通过继承去实现 Serializable 的对象不能识别的 bug;
2.0.3 优化异常提示
2.0.4 修复枚举类型保存的时候不能识别的问题

2.1.0 增加对 PersistableBundle 的支持,NeedSave 注解中设置 isPersistable = true 则说明该参数保存到 PersistableBundle
2.2.6 增加对自定义 view 的数据保存以及恢复
3.0.0 增加 autosave plugin,省去 SaveHelper.save 和 SaveHelper.recover 的调用
3.0.6 简化引入方式
3.0.7 添加对 kotlin 的支持
4.0.2 迁移至 jitpack,支持 gradle tools 4.0+

引入方式

在 project 的 gradle 加入下面的依赖:

repositories {
    maven { url 'https://jitpack.io' }
}

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    ...
    classpath 'com.github.JavaNoober.AutoSave:savehelper-plugin:4.0.2'
}

在 app 的 gradle 或者 module 的 gradle 中加入下面插件即可:

apply plugin: 'AutoSave'

如果需要支持 kotlin:
project 的 gradle:

dependencies {
    ...
    classpath 'com.github.JavaNoober.AutoSave:savehelper-plugin:4.0.2'
    classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10'
}

在 app 的 gradle 或者 module 的 gradle:

apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
apply plugin: 'AutoSave'
apply plugin: 'android-aspectjx'

android{
    ...
    aspectjx{
        include "com.noober.savehelper"
        exclude 'versions.9'
    }

}

混淆配置:

 -dontwarn  com.noober.**
 -keep class com.noober.api.**{*;}
 -keep class com.noober.savehelper.**{*;}
 -keep class * implements com.noober.savehelper.ISaveInstanceStateHelper {*;}

使用方法

对变量增加@NeedSave 注解即可

Activity 和 Fragment

注意:
1.Activity 和 Fragment 中使用的时候必须重写一下onSaveInstanceState 方法,或者在父类 BaseActivity 和 BaseFragment 中重写一次即可,否则保存数据的代码会注入失败
2.如果想要自己定义内存恢复的位置可以使用SaveHelper.recover方法 3.如果要在 kotlin 使用,与在 java 中使用相同,直接加注解即可,但是不同之出在于:
4.1:如果是基本数据类型,需要多添加一个注解@JvmField 4.2:如果是其他数据类型,需要增加 lateinit 关键字或者添加一个注解@JvmField 否则会报错"the modifier of the field must not be private, otherwise it won't work"。

public class MainActivity extends AppCompatActivity {

    @NeedSave
    int a = 0;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.tv);
        textView.setText(a + "");
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                a = 2222;
                textView.setText(a + "");
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        textView.setText(a + "");
        Log.e("MainActivity", a + "");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
}


public class BlankFragment extends Fragment {

    @NeedSave
    String mParam1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
    }
}

class KotlinActivity : AppCompatActivity() {

    @NeedSave
    @JvmField
    var a :Int=3

    @NeedSave
    lateinit var bundle: Bundle

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kotlin)
        Log.e("KotlinActivity",  a.toString())

    }


    override fun onSaveInstanceState(outState: Bundle?) {
        Log.e("KotlinActivity",  "onSaveInstanceState")
        a = 2
        super.onSaveInstanceState(outState)
    }
}

自定义 view

public class CustomView extends View {

    @NeedSave
    int a;

    public CustomView(Context context) {
        super(context);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
        SaveHelper.save(this, container);
        super.dispatchSaveInstanceState(container);
    }

    @Override
    protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
        super.dispatchRestoreInstanceState(container);
        SaveHelper.recover(this, container);
    }
}

具体原理介绍

原理介绍

使用注意

1、如果出现下面的错误,是由 aspectj 导致的,可以看一下解决issue

java.lang.RuntimeException: Unable to instantiate application xxxxx
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools