Now that we have a sweet MVP-ready architecture, how do we integrate these two libraries for view injection and instance state restoration when dealing with Activities/views? The simple answer to this question is InjectingActivity (class that will be inherited in the code by all Activities/views)

public abstract class InjectingActivity extends AppCompatActivity implements MVPView {

   @Override
   public void onCreate(final Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(getLayoutResId());
      ButterKnife.bind(this);
      Icepick.restoreInstanceState(this, savedInstanceState);
   }

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

   /**
    * Provides the layout resource id
    *
    * @return layout resource id
    */
   @LayoutRes
   protected abstract int getLayoutResId();

   /**
    * Provides the context
    *
    * @return context
    */
   @Override
   public Context getContext() {
      return this;
   }
}

Butterknife is required to bind the activity in the onCreate, where Icepick takes care of restoring the state of data (and saves it with the call in the onSaveInstanceState)

Also, there is a method called getLayoutResId() to avoid having to always type setContentView(…) [not really necessary but I like it]. Also, since this extends the MVPView interface, we also override getContext()

 

Butterknife at work

The way Butterknife works (for this app, but it has more functionalities you guys can know more about here) is via an annotation we need to put on each Activity’s view

@BindView(R.id.button_ok)
Button buttonOk;

 

Icepick at work

To have Icepick work properly we need to mark the specific data we want to save the state of with the annotation @State as written below

@State
ArrayList<String> data;

Next topic will be configuration of Retrofit2

Leave a Comment