MakerModder
MakerModder

Reputation: 1571

Force a developer to implement a fragment callback

Hi I have an issue where my coworkers frequently forget to implement the callback to my widely used generic internet error fragment. I have the issue where we only find out a class is missing it when we evoke the fragment. I.E. the application mysteriously crashes and we waste time looking up why.

Short of implementing custom lint for android how can I force a developer to implement the callback? This is a simple fragment that just shows text and takes no arguments.

 @Override
public void onAttach(Context context) {
    super.onAttach(context);
    try {
        mNoInternetCallbackInterface = (NoInternetCallbackInterface) getActivity();
    } catch (ClassCastException e) {
        throw new ClassCastException(getActivity().toString()
                + " must implement OnFragmentInteractionListener");
    }
}



@Override
public void onStart(){
    super.onStart();
    try {
        mNoInternetCallbackInterface = (NoInternetCallbackInterface) getActivity();
    } catch (ClassCastException e) {
        throw new ClassCastException(getActivity().toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mNoInternetCallbackInterface = null;
}

Evoked here in the activity. (yes I know I should use a create instance function instead, but wont solve this issue)

        @Override
        public void onError(final Throwable e) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            NoInternetDialogFragment mNoInternetDialogFragment = new NoInternetDialogFragment();
            try {
                mNoInternetDialogFragment.show(fragmentManager, "no_internet");
            } catch (IllegalStateException ie) {
                // ignore as this code is going away.
            }

        }

Upvotes: 3

Views: 131

Answers (1)

Huaying
Huaying

Reputation: 151

You could put anything that's necessary in a parent fragment, and make other fragments extend parent fragment.We can add abstract method to force user to implement it.

abstract public class ParentFragment extends Fragment {
    //put anything needs to be inherited in this class

    public void onError(final Throwable e) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        NoInternetDialogFragment noInternetDialogFragment = new NoInternetDialogFragment();
        try {
            mNoInternetDialogFragment.show(fragmentManager, "no_internet");
            onErrorHandle(fragmentManager, noInternetDialogFragment);
        } catch (IllegalStateException ie) {
            // ignore as this code is going away.
        }

    }
    abstract protect void onErrorHandle(FragmentManager e, NoInternetDialogFragment fragment);
}

Here's child fragment:

public class ChildFragment extends ParentFragment {

     void onErrorHandle(FragmentManager e, NoInternetDialogFragment fragment) {//your code}
}

So it's ParentFragment that'll handle the case.

Upvotes: 3

Related Questions