Git Product home page Git Product logo

injectorlib's Introduction

InjectorLib

This library allows you to save any primitive, Serializable or Parcelable type objects.

Integration with Gradle

compile 'com.azoft.injector:injector:0.9.3'

To work with this library you should:

  • Create Injector instance with Injector.init call;
  • Call injector.applyRestoreInstanceState(Bundle) with null or bundle object from your Activity or Fragment instance;
  • Call injector.applyOnSaveInstanceState() with bundle object from your Activity or Fragment instance;

Basicly you should create base class, do steps above and Injector will work with any subclass of this base class.

Example of usage

public class DataActivity extends BaseActivity {

    @InjectSavedState
    private Double mValue;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (null == mValue) {
        	mValue = // do init here
        }
        performValueWork(mValue);
    }
}

Bellow are examples of Base classes creation.

Example of usage with Activity.

public class BaseActivity extends Activity {

    private final Injector mInjector = Injector.init(getClass());

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mInjector.applyRestoreInstanceState(this, savedInstanceState);
    }

    @Override
    protected void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);
        
        mInjector.applyOnSaveInstanceState(this, outState);
    }
}

Example of usage with Fragment.

public class BaseFragment extends Fragment {

    private final Injector mInjector = Injector.init(getClass());

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mInjector.applyRestoreInstanceState(this, savedInstanceState);
    }

    @Override
    public void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);

        mInjector.applyOnSaveInstanceState(this, outState);
    }
}

Example of usage with View.

public class BaseCustomView extends View {

    private final Injector mInjector = Injector.init(getClass());

    @Override
    protected void onRestoreInstanceState(final Parcelable state) {
        if (state instanceof InjectorViewSaveState) {
            final InjectorViewSaveState injectorViewSaveState = (InjectorViewSaveState) state;

            mInjector.applyRestoreInstanceState(this, injectorViewSaveState.getOurSaveState());

            super.onRestoreInstanceState(injectorViewSaveState.getSuperState());
        } else {
            super.onRestoreInstanceState(state);
        }
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        return new InjectorViewSaveState(mInjector, this, super.onSaveInstanceState());
    }
}

Example of usage with any other Objects.

public class AnyObject {

private final Injector mInjector = Injector.init(getClass());

    public final void onRestoreInstanceState(final Bundle state) {
    	mInjector.applyRestoreInstanceState(this, state);
    }

    public final void onSaveInstanceState(final Bundle outState) {
        mInjector.applyOnSaveInstanceState(this, outState);
    }
}

Custom tags

Custom tag for Bundle can be generated by implementing InjectSaveStateTag. It is usefull for multiply instances of the same ViewModels or Views that can be instantiated on the screen.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.