项目作者: JavaNoober

项目描述 :
A framework can automatically generate OnSaveInstanceState code, support kotlin.自动生成内存恢复的框架,支持kotlin的使用
高级语言: Java
项目地址: git://github.com/JavaNoober/AutoSave.git
创建时间: 2017-01-09T15:43:35Z
项目社区:https://github.com/JavaNoober/AutoSave

开源协议:

下载


license
JCenter

AutoSave

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

  1. 版本更新说明:
  2. 1.0.0 完成基本功能;
  3. 1.0.1 全局变量的作用域从之前强制public改成只要非private即可;
  4. 1.0.2 修改 SaveHelper.bind(this, savedInstanceState)方法为SaveHelper.recover(this, savedInstanceState),只是重命名,
  5. 以便于理解;
  6. 去掉当内存被回收去调用recover方法时,却没有对应helper类会主动抛异常的情况,方便在BaseAcitviy BaseFragment
  7. onSaveInstanceState onRestoreInstanceState 统一添加SaveHelper.saveSaveHelper.recover方法。
  8. 1.0.3 优化代码生成,如果一个activity或者fragment中没有有效的@NeedSave注解,但是添加了SaveHelper.recoverSaveHelper.save
  9. 方法,现在就不会自动生成这个类的SaveStateHelper类,减少了无用SaveStateHelper类,便于在Base类中统一集成。
  10. 2.0.0 去掉NeedSave注解中的isParcelable字段,自动可以支持不同类型;
  11. 如果字段被标记为private在编译的时候会抛异常;
  12. 支持基本所有bundle可以传入的类型,包括SparseParcelableArray等, 如果传入的类型bundle不支持会抛异常(如果有遗漏的类型,请在github 提出issue);
  13. 2.0.2 修复通过继承去实现Serializable的对象不能识别的bug;
  14. 2.0.3 优化异常提示
  15. 2.0.4 修复枚举类型保存的时候不能识别的问题
  16. 2.1.0 增加对PersistableBundle的支持,NeedSave注解中设置isPersistable = true则说明该参数保存到PersistableBundle
  17. 2.2.6 增加对自定义view的数据保存以及恢复
  18. 3.0.0 增加autosave plugin,省去SaveHelper.save SaveHelper.recover的调用
  19. 3.0.6 简化引入方式
  20. 3.0.7 添加对kotlin的支持
  21. 4.0.2 迁移至jitpack,支持gradle tools 4.0+

引入方式

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

  1. repositories {
  2. maven { url 'https://jitpack.io' }
  3. }
  4. allprojects {
  5. repositories {
  6. maven { url 'https://jitpack.io' }
  7. }
  8. }
  9. dependencies {
  10. ...
  11. classpath 'com.github.JavaNoober.AutoSave:savehelper-plugin:4.0.2'
  12. }

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

  1. apply plugin: 'AutoSave'

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

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

在app的gradle或者module的gradle:

  1. apply plugin: 'kotlin-kapt'
  2. apply plugin: 'kotlin-android-extensions'
  3. apply plugin: 'kotlin-android'
  4. apply plugin: 'AutoSave'
  5. apply plugin: 'android-aspectjx'
  6. android{
  7. ...
  8. aspectjx{
  9. include "com.noober.savehelper"
  10. exclude 'versions.9'
  11. }
  12. }

混淆配置:

  1. -dontwarn com.noober.**
  2. -keep class com.noober.api.**{*;}
  3. -keep class com.noober.savehelper.**{*;}
  4. -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”。

  1. public class MainActivity extends AppCompatActivity {
  2. @NeedSave
  3. int a = 0;
  4. TextView textView;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. textView = findViewById(R.id.tv);
  10. textView.setText(a + "");
  11. textView.setOnClickListener(new View.OnClickListener() {
  12. @Override
  13. public void onClick(View view) {
  14. a = 2222;
  15. textView.setText(a + "");
  16. }
  17. });
  18. }
  19. @Override
  20. protected void onResume() {
  21. super.onResume();
  22. textView.setText(a + "");
  23. Log.e("MainActivity", a + "");
  24. }
  25. @Override
  26. protected void onSaveInstanceState(Bundle outState) {
  27. super.onSaveInstanceState(outState);
  28. }
  29. }
  30. public class BlankFragment extends Fragment {
  31. @NeedSave
  32. String mParam1;
  33. @Override
  34. public void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. }
  37. @Override
  38. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  39. Bundle savedInstanceState) {
  40. return inflater.inflate(R.layout.fragment_blank, container, false);
  41. }
  42. @Override
  43. public void onSaveInstanceState(@NonNull Bundle outState) {
  44. super.onSaveInstanceState(outState);
  45. }
  46. }
  47. class KotlinActivity : AppCompatActivity() {
  48. @NeedSave
  49. @JvmField
  50. var a :Int=3
  51. @NeedSave
  52. lateinit var bundle: Bundle
  53. override fun onCreate(savedInstanceState: Bundle?) {
  54. super.onCreate(savedInstanceState)
  55. setContentView(R.layout.activity_kotlin)
  56. Log.e("KotlinActivity", a.toString())
  57. }
  58. override fun onSaveInstanceState(outState: Bundle?) {
  59. Log.e("KotlinActivity", "onSaveInstanceState")
  60. a = 2
  61. super.onSaveInstanceState(outState)
  62. }
  63. }

自定义view

  1. public class CustomView extends View {
  2. @NeedSave
  3. int a;
  4. public CustomView(Context context) {
  5. super(context);
  6. }
  7. public CustomView(Context context, @Nullable AttributeSet attrs) {
  8. super(context, attrs);
  9. }
  10. public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  11. super(context, attrs, defStyleAttr);
  12. }
  13. @Override
  14. protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
  15. SaveHelper.save(this, container);
  16. super.dispatchSaveInstanceState(container);
  17. }
  18. @Override
  19. protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
  20. super.dispatchRestoreInstanceState(container);
  21. SaveHelper.recover(this, container);
  22. }
  23. }

具体原理介绍

原理介绍

使用注意

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

  1. java.lang.RuntimeException: Unable to instantiate application xxxxx