@Test public void testSaveState() throws Exception { final TestModels.SubClassAllModifiersAnnotatedFields saveStateObject = new TestModels.SubClassAllModifiersAnnotatedFields(); final Bundle stateBundle = new Bundle(); final DefaultFieldsRetriever retriever = new DefaultFieldsRetriever(); Field[] fields = retriever.getFields(saveStateObject.getClass()); for (Field field : fields) { new FieldAccessibleRunner(field) { @Override public void runWithField(@NonNull Field field) throws Exception { field.set(saveStateObject, mRandomizer.nextObject(field.getType())); } }; } Lyra.instance().saveState(saveStateObject, stateBundle); List<Object> fieldValues = listOfValuesFromFields(saveStateObject, fields); List<Object> savedValues = new ArrayList<>(fieldValues.size()); Bundle lyraBundle = stateBundle.getBundle(Lyra.SUB_BUNDLE_KEY); assertNotNull(lyraBundle); for (String key : lyraBundle.keySet()) { savedValues.add(lyraBundle.get(key)); } assertThat(savedValues, containsInAnyOrder(fieldValues.toArray())); }
public void saveState(@NonNull Object stateHolder, @NonNull Bundle state) { final Lyra lyra = instance(); Field[] fields = lyra.mFieldsRetriever.getFields(stateHolder.getClass()); Bundle lyraBundle = new Bundle(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; SaveState saveState = getSaveStateAnnotation(field); boolean accessible = field.isAccessible(); if (!accessible) { field.setAccessible(true); } final Object fieldValue; try { fieldValue = field.get(stateHolder); } catch (IllegalAccessException e) { throw runtimeIllegalAccessException(field); } if (fieldValue != null) { final StateCoder stateCoder; try { stateCoder = lyra.mCoderRetriever.getCoder(saveState, field.getType()); } catch (CoderNotFoundException e) { throw new RuntimeException(e); } stateCoder.serialize(lyraBundle, getKeyFromField(field), fieldValue); } if (!accessible) { field.setAccessible(false); } } state.putBundle(SUB_BUNDLE_KEY, lyraBundle); }
Lyra { public void saveState(@NonNull Object stateHolder, @NonNull Bundle state) { final Lyra lyra = instance(); Field[] fields = lyra.mFieldsRetriever.getFields(stateHolder.getClass()); Bundle lyraBundle = new Bundle(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; SaveState saveState = getSaveStateAnnotation(field); boolean accessible = field.isAccessible(); if (!accessible) { field.setAccessible(true); } final Object fieldValue; try { fieldValue = field.get(stateHolder); } catch (IllegalAccessException e) { throw runtimeIllegalAccessException(field); } if (fieldValue != null) { final StateCoder stateCoder; try { stateCoder = lyra.mCoderRetriever.getCoder(saveState, field.getType()); } catch (CoderNotFoundException e) { throw new RuntimeException(e); } stateCoder.serialize(lyraBundle, getKeyFromField(field), fieldValue); } if (!accessible) { field.setAccessible(false); } } state.putBundle(SUB_BUNDLE_KEY, lyraBundle); } }
Lyra { public void saveState(@NonNull Object stateHolder, @NonNull Bundle state) { final Lyra lyra = instance(); Field[] fields = lyra.mFieldsRetriever.getFields(stateHolder.getClass()); Bundle lyraBundle = new Bundle(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; SaveState saveState = getSaveStateAnnotation(field); boolean accessible = field.isAccessible(); if (!accessible) { field.setAccessible(true); } final Object fieldValue; try { fieldValue = field.get(stateHolder); } catch (IllegalAccessException e) { throw runtimeIllegalAccessException(field); } if (fieldValue != null) { final StateCoder stateCoder; try { stateCoder = lyra.mCoderRetriever.getCoder(saveState, field.getType()); } catch (CoderNotFoundException e) { throw new RuntimeException(e); } stateCoder.serialize(lyraBundle, getKeyFromField(field), fieldValue); } if (!accessible) { field.setAccessible(false); } } state.putBundle(SUB_BUNDLE_KEY, lyraBundle); } @SuppressLint("NewApi") private  Lyra(@NonNull Application application,
                 @NonNull CoderRetriever coderRetriever,
                 @NonNull FieldsRetriever fieldsRetriever,
                 boolean autoSaveActivities); }
Lyra { public void saveState(@NonNull Object stateHolder, @NonNull Bundle state) { final Lyra lyra = instance(); Field[] fields = lyra.mFieldsRetriever.getFields(stateHolder.getClass()); Bundle lyraBundle = new Bundle(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; SaveState saveState = getSaveStateAnnotation(field); boolean accessible = field.isAccessible(); if (!accessible) { field.setAccessible(true); } final Object fieldValue; try { fieldValue = field.get(stateHolder); } catch (IllegalAccessException e) { throw runtimeIllegalAccessException(field); } if (fieldValue != null) { final StateCoder stateCoder; try { stateCoder = lyra.mCoderRetriever.getCoder(saveState, field.getType()); } catch (CoderNotFoundException e) { throw new RuntimeException(e); } stateCoder.serialize(lyraBundle, getKeyFromField(field), fieldValue); } if (!accessible) { field.setAccessible(false); } } state.putBundle(SUB_BUNDLE_KEY, lyraBundle); } @SuppressLint("NewApi") private  Lyra(@NonNull Application application,
                 @NonNull CoderRetriever coderRetriever,
                 @NonNull FieldsRetriever fieldsRetriever,
                 boolean autoSaveActivities); static Builder with(@NonNull Application application); @SuppressLint("NewApi") static void destroy(); static boolean isInitialized(); @NonNull static Lyra instance(); @NonNull static String getKeyFromField(@NonNull Field field); void saveState(@NonNull Object stateHolder, @NonNull Bundle state); void restoreState(@NonNull Object stateHolder, @Nullable Bundle state); Parcelable saveState(@NonNull View stateHolder, @Nullable Parcelable state); Parcelable restoreState(@NonNull View stateHolder, @Nullable Parcelable state); }
Lyra { public void saveState(@NonNull Object stateHolder, @NonNull Bundle state) { final Lyra lyra = instance(); Field[] fields = lyra.mFieldsRetriever.getFields(stateHolder.getClass()); Bundle lyraBundle = new Bundle(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; SaveState saveState = getSaveStateAnnotation(field); boolean accessible = field.isAccessible(); if (!accessible) { field.setAccessible(true); } final Object fieldValue; try { fieldValue = field.get(stateHolder); } catch (IllegalAccessException e) { throw runtimeIllegalAccessException(field); } if (fieldValue != null) { final StateCoder stateCoder; try { stateCoder = lyra.mCoderRetriever.getCoder(saveState, field.getType()); } catch (CoderNotFoundException e) { throw new RuntimeException(e); } stateCoder.serialize(lyraBundle, getKeyFromField(field), fieldValue); } if (!accessible) { field.setAccessible(false); } } state.putBundle(SUB_BUNDLE_KEY, lyraBundle); } @SuppressLint("NewApi") private  Lyra(@NonNull Application application,
                 @NonNull CoderRetriever coderRetriever,
                 @NonNull FieldsRetriever fieldsRetriever,
                 boolean autoSaveActivities); static Builder with(@NonNull Application application); @SuppressLint("NewApi") static void destroy(); static boolean isInitialized(); @NonNull static Lyra instance(); @NonNull static String getKeyFromField(@NonNull Field field); void saveState(@NonNull Object stateHolder, @NonNull Bundle state); void restoreState(@NonNull Object stateHolder, @Nullable Bundle state); Parcelable saveState(@NonNull View stateHolder, @Nullable Parcelable state); Parcelable restoreState(@NonNull View stateHolder, @Nullable Parcelable state); static final String SUB_BUNDLE_KEY; static final String VIEW_SUPER_STATE_BUNDLE_KEY; }