@Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
    mPrefs = new SelectProgramFragmentPreferences(getActivity().getApplicationContext());

    mListView = (ListView) view.findViewById(R.id.event_listview);
    mAdapter = new TrackedEntityInstanceAdapter(getLayoutInflater(savedInstanceState));
    View header =
        getLayoutInflater(savedInstanceState)
            .inflate(R.layout.fragment_select_program_header, mListView, false);
    mProgressBar = (ProgressBar) header.findViewById(R.id.progress_bar);
    mProgressBar.setVisibility(View.GONE);

    mListView.addHeaderView(header, TAG, false);
    mListView.setAdapter(mAdapter);

    mOrgUnitButton = (CardTextViewButton) header.findViewById(R.id.select_organisation_unit);
    mProgramButton = (CardTextViewButton) header.findViewById(R.id.select_program);
    mRegisterEventButton = (FloatingActionButton) header.findViewById(R.id.register_new_event);
    mUpcomingEventsButton = (FloatingActionButton) header.findViewById(R.id.upcoming_events_button);
    mOrgUnitButton.setOnClickListener(this);
    mProgramButton.setOnClickListener(this);
    mRegisterEventButton.setOnClickListener(this);
    mUpcomingEventsButton.setOnClickListener(this);

    mOrgUnitButton.setEnabled(true);
    mProgramButton.setEnabled(false);
    mRegisterEventButton.hide();
    mUpcomingEventsButton.hide();

    if (savedInstanceState != null && savedInstanceState.getParcelable(STATE) != null) {
      mState = savedInstanceState.getParcelable(STATE);
    }

    if (mState == null) {
      // restoring last selection of program
      Pair<String, String> orgUnit = mPrefs.getOrgUnit();
      Pair<String, String> program = mPrefs.getProgram();
      mState = new SelectProgramFragmentState();
      if (orgUnit != null) {
        mState.setOrgUnit(orgUnit.first, orgUnit.second);
        if (program != null) {
          mState.setProgram(program.first, program.second);
        }
      }
    }

    onRestoreState(true);
  }
  public void onUnitSelected(String orgUnitId, String orgUnitLabel) {
    mOrgUnitButton.setText(orgUnitLabel);
    mProgramButton.setEnabled(true);

    mState.setOrgUnit(orgUnitId, orgUnitLabel);
    mState.resetProgram();

    mPrefs.putOrgUnit(new Pair<>(orgUnitId, orgUnitLabel));
    mPrefs.putProgram(null);

    handleViews(0);
  }
  public void onRestoreState(boolean hasUnits) {
    mOrgUnitButton.setEnabled(hasUnits);
    if (!hasUnits) {
      return;
    }

    SelectProgramFragmentState backedUpState = new SelectProgramFragmentState(mState);
    if (!backedUpState.isOrgUnitEmpty()) {
      onUnitSelected(backedUpState.getOrgUnitId(), backedUpState.getOrgUnitLabel());

      if (!backedUpState.isProgramEmpty()) {
        onProgramSelected(backedUpState.getProgramId(), backedUpState.getProgramName());
      }
    }
  }