Skip to content

Commit 36e077f

Browse files
maskaravivekmisaochan
authored andcommitted
Fix crash because of inactive fragment UI (#2046)
* Fix crash because of inactive fragment UI * Add java docs
1 parent 2bc9492 commit 36e077f

File tree

11 files changed

+45
-21
lines changed

11 files changed

+45
-21
lines changed

app/src/main/java/fr/free/nrw/commons/auth/AuthenticatedActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected void showBlockStatus()
6464
.subscribeOn(Schedulers.io())
6565
.observeOn(AndroidSchedulers.mainThread())
6666
.filter(result -> result)
67-
.subscribe(result -> ViewUtil.showSnackbar(findViewById(android.R.id.content), R.string.block_notification)
67+
.subscribe(result -> ViewUtil.showShortSnackbar(findViewById(android.R.id.content), R.string.block_notification)
6868
);
6969
}
7070
}

app/src/main/java/fr/free/nrw/commons/bookmarks/pictures/BookmarkPicturesFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private void handleNoInternet() {
129129
statusTextView.setVisibility(VISIBLE);
130130
statusTextView.setText(getString(R.string.no_internet));
131131
} else {
132-
ViewUtil.showSnackbar(parentLayout, R.string.no_internet);
132+
ViewUtil.showShortSnackbar(parentLayout, R.string.no_internet);
133133
}
134134
}
135135

@@ -140,7 +140,7 @@ private void handleNoInternet() {
140140
private void handleError(Throwable throwable) {
141141
Timber.e(throwable, "Error occurred while loading images inside a category");
142142
try{
143-
ViewUtil.showSnackbar(parentLayout, R.string.error_loading_images);
143+
ViewUtil.showShortSnackbar(parentLayout, R.string.error_loading_images);
144144
initErrorView();
145145
}catch (Exception e){
146146
e.printStackTrace();

app/src/main/java/fr/free/nrw/commons/category/CategoryImagesListFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private void handleNoInternet() {
124124
statusTextView.setVisibility(VISIBLE);
125125
statusTextView.setText(getString(R.string.no_internet));
126126
} else {
127-
ViewUtil.showSnackbar(parentLayout, R.string.no_internet);
127+
ViewUtil.showShortSnackbar(parentLayout, R.string.no_internet);
128128
}
129129
}
130130

@@ -135,7 +135,7 @@ private void handleNoInternet() {
135135
private void handleError(Throwable throwable) {
136136
Timber.e(throwable, "Error occurred while loading images inside a category");
137137
try{
138-
ViewUtil.showSnackbar(parentLayout, R.string.error_loading_images);
138+
ViewUtil.showShortSnackbar(parentLayout, R.string.error_loading_images);
139139
initErrorView();
140140
}catch (Exception e){
141141
e.printStackTrace();

app/src/main/java/fr/free/nrw/commons/category/SubCategoryListFragment.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ private void handleSuccess(List<String> subCategoryList) {
135135
private void handleError(Throwable throwable) {
136136
if (!isParentCategory){
137137
Timber.e(throwable, "Error occurred while loading queried subcategories");
138-
ViewUtil.showSnackbar(categoriesRecyclerView,R.string.error_loading_categories);
138+
ViewUtil.showShortSnackbar(categoriesRecyclerView,R.string.error_loading_categories);
139139
}else {
140140
Timber.e(throwable, "Error occurred while loading queried parentcategories");
141-
ViewUtil.showSnackbar(categoriesRecyclerView,R.string.error_loading_categories);
141+
ViewUtil.showShortSnackbar(categoriesRecyclerView,R.string.error_loading_categories);
142142
}
143143
}
144144

@@ -161,6 +161,6 @@ private void initEmptyView() {
161161
*/
162162
private void handleNoInternet() {
163163
progressBar.setVisibility(GONE);
164-
ViewUtil.showSnackbar(categoriesRecyclerView, R.string.no_internet);
164+
ViewUtil.showShortSnackbar(categoriesRecyclerView, R.string.no_internet);
165165
}
166166
}

app/src/main/java/fr/free/nrw/commons/explore/categories/SearchCategoryFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private void handleError(Throwable throwable) {
196196
Timber.e(throwable, "Error occurred while loading queried categories");
197197
try {
198198
initErrorView();
199-
ViewUtil.showSnackbar(categoriesRecyclerView, R.string.error_loading_categories);
199+
ViewUtil.showShortSnackbar(categoriesRecyclerView, R.string.error_loading_categories);
200200
}catch (Exception e){
201201
e.printStackTrace();
202202
}
@@ -217,6 +217,6 @@ private void initErrorView() {
217217
*/
218218
private void handleNoInternet() {
219219
progressBar.setVisibility(GONE);
220-
ViewUtil.showSnackbar(categoriesRecyclerView, R.string.no_internet);
220+
ViewUtil.showShortSnackbar(categoriesRecyclerView, R.string.no_internet);
221221
}
222222
}

app/src/main/java/fr/free/nrw/commons/explore/images/SearchImageFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ private void handleSuccess(List<Media> mediaList) {
206206
private void handleError(Throwable throwable) {
207207
Timber.e(throwable, "Error occurred while loading queried images");
208208
try {
209-
ViewUtil.showSnackbar(imagesRecyclerView, R.string.error_loading_images);
209+
ViewUtil.showShortSnackbar(imagesRecyclerView, R.string.error_loading_images);
210210
}catch (Exception e){
211211
e.printStackTrace();
212212
}
@@ -226,7 +226,7 @@ private void initErrorView() {
226226
*/
227227
private void handleNoInternet() {
228228
progressBar.setVisibility(GONE);
229-
ViewUtil.showSnackbar(imagesRecyclerView, R.string.no_internet);
229+
ViewUtil.showShortSnackbar(imagesRecyclerView, R.string.no_internet);
230230
}
231231

232232
/**

app/src/main/java/fr/free/nrw/commons/nearby/NearbyFragment.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import android.support.v4.app.FragmentTransaction;
1616
import android.support.v7.app.AlertDialog;
1717
import android.view.LayoutInflater;
18-
1918
import android.view.View;
2019
import android.view.ViewGroup;
2120
import android.widget.LinearLayout;
@@ -37,6 +36,7 @@
3736
import fr.free.nrw.commons.location.LatLng;
3837
import fr.free.nrw.commons.location.LocationServiceManager;
3938
import fr.free.nrw.commons.location.LocationUpdateListener;
39+
import fr.free.nrw.commons.utils.FragmentUtils;
4040
import fr.free.nrw.commons.utils.NetworkUtils;
4141
import fr.free.nrw.commons.utils.UriSerializer;
4242
import fr.free.nrw.commons.utils.ViewUtil;
@@ -323,7 +323,7 @@ private void populatePlaces(NearbyController.NearbyPlacesInfo nearbyPlacesInfo)
323323
String gsonBoundaryCoordinates = gson.toJson(boundaryCoordinates);
324324

325325
if (placeList.size() == 0) {
326-
ViewUtil.showSnackbar(view.findViewById(R.id.container), R.string.no_nearby);
326+
ViewUtil.showShortSnackbar(view.findViewById(R.id.container), R.string.no_nearby);
327327
}
328328

329329
bundle.putString("PlaceList", gsonPlaceList);
@@ -591,14 +591,17 @@ private void showErrorMessage(String message) {
591591
}
592592

593593
private void addNetworkBroadcastReceiver() {
594+
if (!FragmentUtils.isFragmentUIActive(this)) {
595+
return;
596+
}
597+
594598
IntentFilter intentFilter = new IntentFilter(NETWORK_INTENT_ACTION);
595-
snackbar = Snackbar.make(transparentView , R.string.no_internet, Snackbar.LENGTH_INDEFINITE);
596-
597-
broadcastReceiver = new BroadcastReceiver() {
599+
snackbar = Snackbar.make(transparentView, R.string.no_internet, Snackbar.LENGTH_INDEFINITE);
598600

601+
broadcastReceiver = new BroadcastReceiver() {
599602
@Override
600603
public void onReceive(Context context, Intent intent) {
601-
if (snackbar != null) {
604+
if (snackbar != null && getActivity() != null) {
602605
if (NetworkUtils.isInternetConnectionEstablished(getActivity())) {
603606
refreshView(LOCATION_SIGNIFICANTLY_CHANGED);
604607
snackbar.dismiss();
@@ -609,6 +612,10 @@ public void onReceive(Context context, Intent intent) {
609612
}
610613
};
611614

615+
if (getActivity() == null) {
616+
return;
617+
}
618+
612619
getActivity().registerReceiver(broadcastReceiver, intentFilter);
613620

614621
}

app/src/main/java/fr/free/nrw/commons/notification/NotificationActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private void addNotifications() {
106106
progressBar.setVisibility(View.GONE);
107107
}, throwable -> {
108108
Timber.e(throwable, "Error occurred while loading notifications");
109-
ViewUtil.showSnackbar(relativeLayout, R.string.error_notifications);
109+
ViewUtil.showShortSnackbar(relativeLayout, R.string.error_notifications);
110110
progressBar.setVisibility(View.GONE);
111111
});
112112
} else {
@@ -123,7 +123,7 @@ private void handleUrl(String url) {
123123

124124
private void setAdapter(List<Notification> notificationList) {
125125
if (notificationList == null || notificationList.isEmpty()) {
126-
ViewUtil.showSnackbar(relativeLayout, R.string.no_notifications);
126+
ViewUtil.showShortSnackbar(relativeLayout, R.string.no_notifications);
127127
return;
128128
}
129129
notificationAdapterFactory = new NotificationAdapterFactory(notification -> {

app/src/main/java/fr/free/nrw/commons/utils/FragmentUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,13 @@ public static boolean addAndCommitFragmentWithImmediateExecution(
2828
}
2929
return false;
3030
}
31+
32+
/**
33+
* Utility function to check whether the fragment UI is still active or not
34+
* @param fragment
35+
* @return
36+
*/
37+
public static boolean isFragmentUIActive(Fragment fragment) {
38+
return fragment.getActivity() != null && fragment.isAdded() && !fragment.isDetached() && !fragment.isRemoving();
39+
}
3140
}

app/src/main/java/fr/free/nrw/commons/utils/NetworkUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ public static boolean isInternetConnectionEstablished(Context context) {
1111
ConnectivityManager cm =
1212
(ConnectivityManager)context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
1313

14+
if (cm == null) {
15+
return false;
16+
}
1417
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
1518
return activeNetwork != null &&
1619
activeNetwork.isConnectedOrConnecting();

0 commit comments

Comments
 (0)