livefront / bridge

An Android library for avoiding TransactionTooLargeException during state saving and restoration

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

Hmm, it worked as expected but in any case i have to implement a custom StateManager interface for all old style activities. Better to give same effort for icepick, thank you so much @byencho .