https://developer.android.com/guide/components/fragments.html?hl=ko#java
프래그먼트 | Android 개발자 | Android Developers
A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section…
developer.android.com
Fragment 생명주기에 보면, 다음과 같이 주의사항이 표시.
따라서 안전하게
다음과 같이 onAttach에서 context 와 activity를 복사해두자.
@Override
public void onAttach(Context context) {
mContext = context;
if (context instanceof Activity) {
mActivity = (Activity)context;
}
super.onAttach(context);
}
// detach에서는 변수 clearing 해주기 leak방지
@Override
public void onDetach() {
mActivity = null;
mContext = null;
super.onDetach();
}