Skip to content
josheroff edited this page Jan 28, 2017 · 16 revisions

#View Views are either an Activity or Fragment. Views are dumb classes whose only purpose is to hold the references to the widgets from the XML. They simply receive UI events, such as a click, and pass them off to be handled elsewhere (their presenter). There is minimal to no logic in the entire class due to this delegation of responsibility. In addition to the references to the widgets, Views need a reference to their Presenter, which they get from the Host component (more on Dagger to come).

There is some boilerplate that goes into a Sliver View that would be better suited in a base class, but due to the current Refclient hierarchy of extensions, this wasn't immediately possible. This inconvenience will be phased out as Sliver's relationship with the project evolves.

Example of a Skeleton View is below. Every portion of this code is necessary:

import ....

/**
* Views must implement their Component-specific Contract's View interface
**/
public class SkeletonFragment extends Fragment implements SkeletonContract.View {

    @Inject
    SkeletonComponent mSkeletonComponent;

    private SkeletonContract.Presenter presenter;

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

        //The MainActivity is whoever the Host to this Fragment is
        ((MainActivity) getActivity()).hostComponent.sliverComponent().inject(this);
        presenter = mSkeletonComponent.presenter();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.main_activity, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);

        //Setting the View is required for the Presenter to know who is fulfilling the View Contract
        //In an Activity, one would call this method in the onCreate callback, after setContentView
        presenter.setView(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        //This is to clear the reference to the View to prevent memory leaks
        presenter.onDestroyView();
    }

}
Clone this wiki locally