Simply replace this.fragment.getActivity()
with this.activity
since you already have reference to the activity.
Technically, to make it a more reusable and decoupled class I would replace the reference to MainActivity
and simply call it Context
since that's really all you need it for and don't really care if it's the MainActivity or some other Activity that this later lives on.