@Test public void testRestoreState() throws Exception { final TestModels.SubClassAllModifiersAnnotatedFields saveStateObject = new TestModels.SubClassAllModifiersAnnotatedFields(); final Bundle stateBundle = new Bundle(); final Bundle lyraBundle = new Bundle(); final DefaultFieldsRetriever retriever = new DefaultFieldsRetriever(); final DefaultCoderRetriever coderRetriever = new DefaultCoderRetriever(); Field[] fields = retriever.getFields(saveStateObject.getClass()); List<Object> savedValues = new ArrayList<>(fields.length); for (Field field : fields) { SaveState saveState = field.getAnnotation(SaveState.class); Class<?> fieldType = field.getType(); StateCoder stateCoder = coderRetriever.getCoder(saveState, fieldType); Object randomObj = mRandomizer.nextObject(fieldType); savedValues.add(randomObj); stateCoder.serialize(lyraBundle, Lyra.getKeyFromField(field), randomObj); } stateBundle.putBundle(Lyra.SUB_BUNDLE_KEY, lyraBundle); Lyra.instance().restoreState(saveStateObject, stateBundle); List<Object> fieldValues = listOfValuesFromFields(saveStateObject, fields); assertThat(savedValues, containsInAnyOrder(fieldValues.toArray())); }
public void restoreState(@NonNull Object stateHolder, @Nullable Bundle state) { Bundle lyraBundle; if (state == null || (lyraBundle = state.getBundle(SUB_BUNDLE_KEY)) == null) return; final Lyra lyra = instance(); Field[] fields = lyra.mFieldsRetriever.getFields(stateHolder.getClass()); 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 StateCoder stateCoder; try { stateCoder = lyra.mCoderRetriever.getCoder(saveState, field.getType()); } catch (CoderNotFoundException e) { throw new RuntimeException(e); } final Object fieldValue = stateCoder.deserialize(lyraBundle, getKeyFromField(field)); if (fieldValue != null) { try { field.set(stateHolder, fieldValue); } catch (IllegalAccessException e) { throw runtimeIllegalAccessException(field); } } if (!accessible) { field.setAccessible(false); } } }
Lyra { public void restoreState(@NonNull Object stateHolder, @Nullable Bundle state) { Bundle lyraBundle; if (state == null || (lyraBundle = state.getBundle(SUB_BUNDLE_KEY)) == null) return; final Lyra lyra = instance(); Field[] fields = lyra.mFieldsRetriever.getFields(stateHolder.getClass()); 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 StateCoder stateCoder; try { stateCoder = lyra.mCoderRetriever.getCoder(saveState, field.getType()); } catch (CoderNotFoundException e) { throw new RuntimeException(e); } final Object fieldValue = stateCoder.deserialize(lyraBundle, getKeyFromField(field)); if (fieldValue != null) { try { field.set(stateHolder, fieldValue); } catch (IllegalAccessException e) { throw runtimeIllegalAccessException(field); } } if (!accessible) { field.setAccessible(false); } } } }
Lyra { public void restoreState(@NonNull Object stateHolder, @Nullable Bundle state) { Bundle lyraBundle; if (state == null || (lyraBundle = state.getBundle(SUB_BUNDLE_KEY)) == null) return; final Lyra lyra = instance(); Field[] fields = lyra.mFieldsRetriever.getFields(stateHolder.getClass()); 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 StateCoder stateCoder; try { stateCoder = lyra.mCoderRetriever.getCoder(saveState, field.getType()); } catch (CoderNotFoundException e) { throw new RuntimeException(e); } final Object fieldValue = stateCoder.deserialize(lyraBundle, getKeyFromField(field)); if (fieldValue != null) { try { field.set(stateHolder, fieldValue); } catch (IllegalAccessException e) { throw runtimeIllegalAccessException(field); } } if (!accessible) { field.setAccessible(false); } } } @SuppressLint("NewApi") private  Lyra(@NonNull Application application,
                 @NonNull CoderRetriever coderRetriever,
                 @NonNull FieldsRetriever fieldsRetriever,
                 boolean autoSaveActivities); }
Lyra { public void restoreState(@NonNull Object stateHolder, @Nullable Bundle state) { Bundle lyraBundle; if (state == null || (lyraBundle = state.getBundle(SUB_BUNDLE_KEY)) == null) return; final Lyra lyra = instance(); Field[] fields = lyra.mFieldsRetriever.getFields(stateHolder.getClass()); 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 StateCoder stateCoder; try { stateCoder = lyra.mCoderRetriever.getCoder(saveState, field.getType()); } catch (CoderNotFoundException e) { throw new RuntimeException(e); } final Object fieldValue = stateCoder.deserialize(lyraBundle, getKeyFromField(field)); if (fieldValue != null) { try { field.set(stateHolder, fieldValue); } catch (IllegalAccessException e) { throw runtimeIllegalAccessException(field); } } if (!accessible) { field.setAccessible(false); } } } @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 restoreState(@NonNull Object stateHolder, @Nullable Bundle state) { Bundle lyraBundle; if (state == null || (lyraBundle = state.getBundle(SUB_BUNDLE_KEY)) == null) return; final Lyra lyra = instance(); Field[] fields = lyra.mFieldsRetriever.getFields(stateHolder.getClass()); 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 StateCoder stateCoder; try { stateCoder = lyra.mCoderRetriever.getCoder(saveState, field.getType()); } catch (CoderNotFoundException e) { throw new RuntimeException(e); } final Object fieldValue = stateCoder.deserialize(lyraBundle, getKeyFromField(field)); if (fieldValue != null) { try { field.set(stateHolder, fieldValue); } catch (IllegalAccessException e) { throw runtimeIllegalAccessException(field); } } if (!accessible) { field.setAccessible(false); } } } @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; }