Skip to content

Instantly share code, notes, and snippets.

@ibrahimsn98
Last active August 19, 2018 15:38
Show Gist options
  • Save ibrahimsn98/ec7551108d3ff1ee1c4c81a57ac796ce to your computer and use it in GitHub Desktop.
Save ibrahimsn98/ec7551108d3ff1ee1c4c81a57ac796ce to your computer and use it in GitHub Desktop.
android-mvvm-with-dagger-2
public abstract class BaseFragment extends DaggerFragment {
private Unbinder unbinder;
private AppCompatActivity activity;
@LayoutRes
protected abstract int layoutRes();
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(layoutRes(), container, false);
unbinder = ButterKnife.bind(this, view);
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
activity = (AppCompatActivity) context;
}
@Override
public void onDetach() {
super.onDetach();
activity = null;
}
public AppCompatActivity getBaseActivity() {
return activity;
}
@Override
public void onDestroyView() {
super.onDestroyView();
if(unbinder != null) {
unbinder.unbind();
unbinder = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment