Use bridge without icepick like state handler libraries
asozcan opened this issue · comments
I'm trying to apply this library without any other state handler library. Is there any workaround to implement it compatible with old style savedInstanceState methods when initializing it? Something like;
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Bridge.saveInstanceState(this, outState);
outState.putParcelable("hugeObject", hugeObject);
}
As i test, it causes TransactionTooLarge in any case, if there's no @State annotation for objects. Icepick is super nice but i have almost 200 unique activity in my project and lazy to overhaul all :)
To use Bridge
without an annotation-based library, one way would be to make a new interface like
public interface StateManager {
void restoreState(@Nullable Bundle bundle);
void saveState(@NonNull Bundle bundle);
}
and make your Activities and Fragments implement that and do your actually save-state handling there:
public class MainActivity extends Activity implements StateManager {
...
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Bridge.saveInstanceState(this, outState);
}
@Override
public void saveState(@NonNull Bundle bundle) {
bundle.putParcelable("hugeObject", hugeObject);
}
...
}
Then your Bridge
initialization would look like:
Bridge.initialize(getApplicationContext(), new SavedStateHandler() {
@Override
public void saveInstanceState(@NonNull Object target, @NonNull Bundle state) {
if (target instanceof StateManager) {
((StateManager) target).saveState(state);
}
}
@Override
public void restoreInstanceState(@NonNull Object target, @Nullable Bundle state) {
if (target instanceof StateManager) {
((StateManager) target).restoreState(state);
}
}
});
Something like that should work if you really want to go that route. But personally I think it's just much easier at that point to switch to using a library like Icepick
.