Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bridge without icepick like state handler libraries #14

Closed
asozcan opened this issue May 31, 2018 · 2 comments
Closed

Use bridge without icepick like state handler libraries #14

asozcan opened this issue May 31, 2018 · 2 comments

Comments

@asozcan
Copy link

asozcan commented May 31, 2018

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 :)

@byencho
Copy link

byencho commented May 31, 2018

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.

@asozcan
Copy link
Author

asozcan commented May 31, 2018

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 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants