I have an app with a single activity and two fragments. Fragment B is added(addedToBackStack) on Top of Fragment A. In Fragment B, I am showing a dialog, going back to Fragment A and then dismissing the dialog. If getActivity()!=null
check is removed inside the handler, the code works fine. But getActivity()
is null inside handler. Why is getActivity()
null inside handler in the following piece of code?
private void showDialog(final Dialog dialog) {
dialog.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (getActivity()!=null && !getActivity().isFinishing()) {
dialog.dismiss();
}
}
}, 1000);
if (getActivity() != null && !getActivity().isFinishing())
getActivity().onBackPressed();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…