Fix crash because of inactive fragment UI (#2046)

* Fix crash because of inactive fragment UI

* Add java docs
This commit is contained in:
Vivek Maskara 2018-12-05 13:39:08 +05:30 committed by Josephine Lim
parent 2bc9492367
commit 36e077f0b1
11 changed files with 45 additions and 21 deletions

View file

@ -64,7 +64,7 @@ public abstract class AuthenticatedActivity extends NavigationBaseActivity {
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.filter(result -> result) .filter(result -> result)
.subscribe(result -> ViewUtil.showSnackbar(findViewById(android.R.id.content), R.string.block_notification) .subscribe(result -> ViewUtil.showShortSnackbar(findViewById(android.R.id.content), R.string.block_notification)
); );
} }
} }

View file

@ -129,7 +129,7 @@ public class BookmarkPicturesFragment extends DaggerFragment {
statusTextView.setVisibility(VISIBLE); statusTextView.setVisibility(VISIBLE);
statusTextView.setText(getString(R.string.no_internet)); statusTextView.setText(getString(R.string.no_internet));
} else { } else {
ViewUtil.showSnackbar(parentLayout, R.string.no_internet); ViewUtil.showShortSnackbar(parentLayout, R.string.no_internet);
} }
} }
@ -140,7 +140,7 @@ public class BookmarkPicturesFragment extends DaggerFragment {
private void handleError(Throwable throwable) { private void handleError(Throwable throwable) {
Timber.e(throwable, "Error occurred while loading images inside a category"); Timber.e(throwable, "Error occurred while loading images inside a category");
try{ try{
ViewUtil.showSnackbar(parentLayout, R.string.error_loading_images); ViewUtil.showShortSnackbar(parentLayout, R.string.error_loading_images);
initErrorView(); initErrorView();
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();

View file

@ -124,7 +124,7 @@ public class CategoryImagesListFragment extends DaggerFragment {
statusTextView.setVisibility(VISIBLE); statusTextView.setVisibility(VISIBLE);
statusTextView.setText(getString(R.string.no_internet)); statusTextView.setText(getString(R.string.no_internet));
} else { } else {
ViewUtil.showSnackbar(parentLayout, R.string.no_internet); ViewUtil.showShortSnackbar(parentLayout, R.string.no_internet);
} }
} }
@ -135,7 +135,7 @@ public class CategoryImagesListFragment extends DaggerFragment {
private void handleError(Throwable throwable) { private void handleError(Throwable throwable) {
Timber.e(throwable, "Error occurred while loading images inside a category"); Timber.e(throwable, "Error occurred while loading images inside a category");
try{ try{
ViewUtil.showSnackbar(parentLayout, R.string.error_loading_images); ViewUtil.showShortSnackbar(parentLayout, R.string.error_loading_images);
initErrorView(); initErrorView();
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();

View file

@ -135,10 +135,10 @@ public class SubCategoryListFragment extends CommonsDaggerSupportFragment {
private void handleError(Throwable throwable) { private void handleError(Throwable throwable) {
if (!isParentCategory){ if (!isParentCategory){
Timber.e(throwable, "Error occurred while loading queried subcategories"); Timber.e(throwable, "Error occurred while loading queried subcategories");
ViewUtil.showSnackbar(categoriesRecyclerView,R.string.error_loading_categories); ViewUtil.showShortSnackbar(categoriesRecyclerView,R.string.error_loading_categories);
}else { }else {
Timber.e(throwable, "Error occurred while loading queried parentcategories"); Timber.e(throwable, "Error occurred while loading queried parentcategories");
ViewUtil.showSnackbar(categoriesRecyclerView,R.string.error_loading_categories); ViewUtil.showShortSnackbar(categoriesRecyclerView,R.string.error_loading_categories);
} }
} }
@ -161,6 +161,6 @@ public class SubCategoryListFragment extends CommonsDaggerSupportFragment {
*/ */
private void handleNoInternet() { private void handleNoInternet() {
progressBar.setVisibility(GONE); progressBar.setVisibility(GONE);
ViewUtil.showSnackbar(categoriesRecyclerView, R.string.no_internet); ViewUtil.showShortSnackbar(categoriesRecyclerView, R.string.no_internet);
} }
} }

View file

@ -196,7 +196,7 @@ public class SearchCategoryFragment extends CommonsDaggerSupportFragment {
Timber.e(throwable, "Error occurred while loading queried categories"); Timber.e(throwable, "Error occurred while loading queried categories");
try { try {
initErrorView(); initErrorView();
ViewUtil.showSnackbar(categoriesRecyclerView, R.string.error_loading_categories); ViewUtil.showShortSnackbar(categoriesRecyclerView, R.string.error_loading_categories);
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
} }
@ -217,6 +217,6 @@ public class SearchCategoryFragment extends CommonsDaggerSupportFragment {
*/ */
private void handleNoInternet() { private void handleNoInternet() {
progressBar.setVisibility(GONE); progressBar.setVisibility(GONE);
ViewUtil.showSnackbar(categoriesRecyclerView, R.string.no_internet); ViewUtil.showShortSnackbar(categoriesRecyclerView, R.string.no_internet);
} }
} }

View file

@ -206,7 +206,7 @@ public class SearchImageFragment extends CommonsDaggerSupportFragment {
private void handleError(Throwable throwable) { private void handleError(Throwable throwable) {
Timber.e(throwable, "Error occurred while loading queried images"); Timber.e(throwable, "Error occurred while loading queried images");
try { try {
ViewUtil.showSnackbar(imagesRecyclerView, R.string.error_loading_images); ViewUtil.showShortSnackbar(imagesRecyclerView, R.string.error_loading_images);
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
} }
@ -226,7 +226,7 @@ public class SearchImageFragment extends CommonsDaggerSupportFragment {
*/ */
private void handleNoInternet() { private void handleNoInternet() {
progressBar.setVisibility(GONE); progressBar.setVisibility(GONE);
ViewUtil.showSnackbar(imagesRecyclerView, R.string.no_internet); ViewUtil.showShortSnackbar(imagesRecyclerView, R.string.no_internet);
} }
/** /**

View file

@ -15,7 +15,6 @@ import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.LinearLayout; import android.widget.LinearLayout;
@ -37,6 +36,7 @@ import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
import fr.free.nrw.commons.location.LatLng; import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.location.LocationServiceManager; import fr.free.nrw.commons.location.LocationServiceManager;
import fr.free.nrw.commons.location.LocationUpdateListener; import fr.free.nrw.commons.location.LocationUpdateListener;
import fr.free.nrw.commons.utils.FragmentUtils;
import fr.free.nrw.commons.utils.NetworkUtils; import fr.free.nrw.commons.utils.NetworkUtils;
import fr.free.nrw.commons.utils.UriSerializer; import fr.free.nrw.commons.utils.UriSerializer;
import fr.free.nrw.commons.utils.ViewUtil; import fr.free.nrw.commons.utils.ViewUtil;
@ -323,7 +323,7 @@ public class NearbyFragment extends CommonsDaggerSupportFragment
String gsonBoundaryCoordinates = gson.toJson(boundaryCoordinates); String gsonBoundaryCoordinates = gson.toJson(boundaryCoordinates);
if (placeList.size() == 0) { if (placeList.size() == 0) {
ViewUtil.showSnackbar(view.findViewById(R.id.container), R.string.no_nearby); ViewUtil.showShortSnackbar(view.findViewById(R.id.container), R.string.no_nearby);
} }
bundle.putString("PlaceList", gsonPlaceList); bundle.putString("PlaceList", gsonPlaceList);
@ -591,14 +591,17 @@ public class NearbyFragment extends CommonsDaggerSupportFragment
} }
private void addNetworkBroadcastReceiver() { private void addNetworkBroadcastReceiver() {
if (!FragmentUtils.isFragmentUIActive(this)) {
return;
}
IntentFilter intentFilter = new IntentFilter(NETWORK_INTENT_ACTION); IntentFilter intentFilter = new IntentFilter(NETWORK_INTENT_ACTION);
snackbar = Snackbar.make(transparentView , R.string.no_internet, Snackbar.LENGTH_INDEFINITE); snackbar = Snackbar.make(transparentView, R.string.no_internet, Snackbar.LENGTH_INDEFINITE);
broadcastReceiver = new BroadcastReceiver() {
broadcastReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (snackbar != null) { if (snackbar != null && getActivity() != null) {
if (NetworkUtils.isInternetConnectionEstablished(getActivity())) { if (NetworkUtils.isInternetConnectionEstablished(getActivity())) {
refreshView(LOCATION_SIGNIFICANTLY_CHANGED); refreshView(LOCATION_SIGNIFICANTLY_CHANGED);
snackbar.dismiss(); snackbar.dismiss();
@ -609,6 +612,10 @@ public class NearbyFragment extends CommonsDaggerSupportFragment
} }
}; };
if (getActivity() == null) {
return;
}
getActivity().registerReceiver(broadcastReceiver, intentFilter); getActivity().registerReceiver(broadcastReceiver, intentFilter);
} }

View file

@ -106,7 +106,7 @@ public class NotificationActivity extends NavigationBaseActivity {
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);
}, throwable -> { }, throwable -> {
Timber.e(throwable, "Error occurred while loading notifications"); Timber.e(throwable, "Error occurred while loading notifications");
ViewUtil.showSnackbar(relativeLayout, R.string.error_notifications); ViewUtil.showShortSnackbar(relativeLayout, R.string.error_notifications);
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);
}); });
} else { } else {
@ -123,7 +123,7 @@ public class NotificationActivity extends NavigationBaseActivity {
private void setAdapter(List<Notification> notificationList) { private void setAdapter(List<Notification> notificationList) {
if (notificationList == null || notificationList.isEmpty()) { if (notificationList == null || notificationList.isEmpty()) {
ViewUtil.showSnackbar(relativeLayout, R.string.no_notifications); ViewUtil.showShortSnackbar(relativeLayout, R.string.no_notifications);
return; return;
} }
notificationAdapterFactory = new NotificationAdapterFactory(notification -> { notificationAdapterFactory = new NotificationAdapterFactory(notification -> {

View file

@ -28,4 +28,13 @@ public class FragmentUtils {
} }
return false; return false;
} }
/**
* Utility function to check whether the fragment UI is still active or not
* @param fragment
* @return
*/
public static boolean isFragmentUIActive(Fragment fragment) {
return fragment.getActivity() != null && fragment.isAdded() && !fragment.isDetached() && !fragment.isRemoving();
}
} }

View file

@ -11,6 +11,9 @@ public class NetworkUtils {
ConnectivityManager cm = ConnectivityManager cm =
(ConnectivityManager)context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); (ConnectivityManager)context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) {
return false;
}
NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null && return activeNetwork != null &&
activeNetwork.isConnectedOrConnecting(); activeNetwork.isConnectedOrConnecting();

View file

@ -17,7 +17,12 @@ public class ViewUtil {
public static final String SHOWCASE_VIEW_ID_2 = "SHOWCASE_VIEW_ID_2"; public static final String SHOWCASE_VIEW_ID_2 = "SHOWCASE_VIEW_ID_2";
public static final String SHOWCASE_VIEW_ID_3 = "SHOWCASE_VIEW_ID_3"; public static final String SHOWCASE_VIEW_ID_3 = "SHOWCASE_VIEW_ID_3";
public static void showSnackbar(View view, int messageResourceId) { /**
* Utility function to show short snack bar
* @param view
* @param messageResourceId
*/
public static void showShortSnackbar(View view, int messageResourceId) {
if (view.getContext() == null) { if (view.getContext() == null) {
return; return;
} }