Android:在片段

时间:2017-01-24 19:16:34

标签: android android-fragments inheritance extends method-overriding

我一直在质疑自己如何解决这个问题,但没有找到合适的解决方案。我有以下Fragment的子类:

BaseListFragment extends ListFragment
BaseDialogFragment extends DialogFragment
BaseFragment extends Fragment

这些BaseFragments中的每一个都覆盖相同的方法并实现相同的功能。例如:

public abstract class BaseFragment extends Fragment
{
    private BaseActivity baseActivity;

    @Override
    public void onAttach(Context context)
    {
       super.onAttach(context);
       baseActivity = (BaseActivity) context;
    }

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

    // other overridden methods.
}

public abstract class BaseDialogFragment extends DialogFragment
{
    private BaseActivity baseActivity;

    @Override
    public void onAttach(Context context)
    {
       super.onAttach(context);
       baseActivity = (BaseActivity) context;
    }

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

    // other overridden methods.
}

public abstract class BaseListFragment extends ListFragment
{
    private BaseActivity baseActivity;

    @Override
    public void onAttach(Context context)
    {
       super.onAttach(context);
       baseActivity = (BaseActivity) context;
    }

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

    // other overridden methods.
}

我的应用程序中的所有其他片段然后扩展任何这些片段。但问题是我不想三次编码。

我希望能解决这个问题吗?

0 个答案:

没有答案