Fix #2520: Fix dangling snackbar in NearbyFragment (#2521)

NetworkBroadcastReceiver is unregistered in onPause() and not reregistered in onResume(). This causes the snackbar doesn't disappear after network becomes available. Fixed this and improved the null checking logic in snackbar related code.
This commit is contained in:
Zhao Gang 2019-03-16 07:47:21 +08:00 committed by Adam Jones
parent 5c4278bbff
commit bb40c24de8

View file

@ -152,6 +152,7 @@ public class NearbyFragment extends CommonsDaggerSupportFragment
// Find the retained fragment on activity restarts
nearbyMapFragment = getMapFragment();
nearbyListFragment = getListFragment();
addNetworkBroadcastReceiver();
}
/**
@ -712,20 +713,31 @@ public class NearbyFragment extends CommonsDaggerSupportFragment
return;
}
if (broadcastReceiver != null) {
return;
}
IntentFilter intentFilter = new IntentFilter(NETWORK_INTENT_ACTION);
snackbar = Snackbar.make(transparentView, R.string.no_internet, Snackbar.LENGTH_INDEFINITE);
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (snackbar != null && getActivity() != null) {
if (getActivity() != null) {
if (NetworkUtils.isInternetConnectionEstablished(getActivity())) {
if (isNetworkErrorOccured) {
refreshView(LOCATION_SIGNIFICANTLY_CHANGED);
isNetworkErrorOccured = false;
}
if (snackbar != null) {
snackbar.dismiss();
snackbar = null;
}
} else {
if (snackbar == null) {
snackbar = Snackbar.make(view, R.string.no_internet, Snackbar.LENGTH_INDEFINITE);
}
isNetworkErrorOccured = true;
snackbar.show();
}
@ -733,12 +745,7 @@ public class NearbyFragment extends CommonsDaggerSupportFragment
}
};
if (getActivity() == null) {
return;
}
getActivity().registerReceiver(broadcastReceiver, intentFilter);
}
@Override