frankiesardo / icepick

Android Instance State made easy
Eclipse Public License 1.0
3.75k stars 208 forks source link

Save and restore arbitrary classes. #58

Closed tevjef closed 8 years ago

tevjef commented 9 years ago

Is it possible for the processor to the create an instance of class that implements IcicleClass and use reflection to call it's save() and restore()methods, passing a bundle. I apologize for writing up a use case instead of some proof of concept code. I have no idea where to start with annotation processing.

class ViewState implements IcicleClass {
    @Icicle boolean isRefreshing;
    @Icicle boolean emtyLayoutShowing;

    @Override
    public void save(Bundle savedInstanceState) {
        Icepick.saveInstanceState(this, savedInstanceState);
    }

    @Override
    public void restore(Bundle savedInstanceState) {
       Icepick.restoreInstanceState(this, savedInstanceState);
    }
}

class MyFragment extends Fragment {
    @IcicleClass
    ViewState mViewState;

    @Icicle
    ArrayList<Parcelable> mData;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Icepick.restoreInstanceState(this, savedInstanceState);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Icepick.saveInstanceState(this, outState);
    }
}

Or would it just be easier to make ViewState implement Parcelable. Just an idea.

frankiesardo commented 9 years ago

Apologies but I don't understand where are you aiming at.

Can you take a step back and describe the problem and the proposed solution?