From 63e2ba3493e17017fc9c0dda0585ed598dd068fc Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 12 Mar 2020 12:15:45 +0200 Subject: [PATCH] Fix several possible crashes with fragment manager and import dialogs --- .../activities/FavoritesSearchFragment.java | 6 +++- .../osmand/plus/activities/MapActivity.java | 16 ++++------ ...eoNoteRecordingMenuFullScreenFragment.java | 9 +++++- .../osmand/plus/base/ContextMenuFragment.java | 8 ++--- .../net/osmand/plus/helpers/ImportHelper.java | 32 +++++++++++-------- .../MapContextMenuFragment.java | 6 ++-- .../ContextMenuCardDialogFragment.java | 8 ++--- .../search/QuickSearchDialogFragment.java | 5 ++- .../plus/settings/BaseSettingsFragment.java | 5 ++- 9 files changed, 56 insertions(+), 39 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/FavoritesSearchFragment.java b/OsmAnd/src/net/osmand/plus/activities/FavoritesSearchFragment.java index 2bdd750655..34265a37b2 100644 --- a/OsmAnd/src/net/osmand/plus/activities/FavoritesSearchFragment.java +++ b/OsmAnd/src/net/osmand/plus/activities/FavoritesSearchFragment.java @@ -33,6 +33,7 @@ import androidx.core.content.ContextCompat; import androidx.core.view.ViewCompat; import androidx.fragment.app.DialogFragment; import androidx.fragment.app.FragmentActivity; +import androidx.fragment.app.FragmentManager; import net.osmand.AndroidUtils; import net.osmand.access.AccessibilityAssistant; @@ -221,7 +222,10 @@ public class FavoritesSearchFragment extends DialogFragment { public void onDismiss(DialogInterface dialog) { Activity activity = getActivity(); if (activity != null) { - getChildFragmentManager().popBackStack(); + FragmentManager fragmentManager = getChildFragmentManager(); + if (!fragmentManager.isStateSaved()) { + fragmentManager.popBackStack(); + } } super.onDismiss(dialog); } diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 48726c6d7c..65eda804c7 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -1128,10 +1128,9 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven } public void dismissCardDialog() { - try { - getSupportFragmentManager().popBackStack(ContextMenuCardDialogFragment.TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE); - } catch (Exception e) { - e.printStackTrace(); + FragmentManager fragmentManager = getSupportFragmentManager(); + if (!fragmentManager.isStateSaved()) { + fragmentManager.popBackStack(ContextMenuCardDialogFragment.TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE); } } @@ -2166,10 +2165,9 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven } public void dismissSettingsScreens() { - try { - getSupportFragmentManager().popBackStack(DRAWER_SETTINGS_ID + ".new", FragmentManager.POP_BACK_STACK_INCLUSIVE); - } catch (Exception e) { - e.printStackTrace(); + FragmentManager fragmentManager = getSupportFragmentManager(); + if (!fragmentManager.isStateSaved()) { + fragmentManager.popBackStack(DRAWER_SETTINGS_ID + ".new", FragmentManager.POP_BACK_STACK_INCLUSIVE); } } @@ -2456,7 +2454,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven public void backToConfigureProfileFragment() { FragmentManager fragmentManager = getSupportFragmentManager(); int backStackEntryCount = fragmentManager.getBackStackEntryCount(); - if (backStackEntryCount > 0) { + if (backStackEntryCount > 0 && !fragmentManager.isStateSaved()) { BackStackEntry entry = fragmentManager.getBackStackEntryAt(backStackEntryCount - 1); if (ConfigureProfileFragment.TAG.equals(entry.getName())) { fragmentManager.popBackStack(); diff --git a/OsmAnd/src/net/osmand/plus/audionotes/AudioVideoNoteRecordingMenuFullScreenFragment.java b/OsmAnd/src/net/osmand/plus/audionotes/AudioVideoNoteRecordingMenuFullScreenFragment.java index e38ecfa892..a594c9ec72 100644 --- a/OsmAnd/src/net/osmand/plus/audionotes/AudioVideoNoteRecordingMenuFullScreenFragment.java +++ b/OsmAnd/src/net/osmand/plus/audionotes/AudioVideoNoteRecordingMenuFullScreenFragment.java @@ -8,6 +8,7 @@ import android.view.WindowManager; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; import net.osmand.plus.R; @@ -59,6 +60,12 @@ public class AudioVideoNoteRecordingMenuFullScreenFragment extends Fragment { public void dismiss() { dismissing = true; - getActivity().getSupportFragmentManager().popBackStack(); + FragmentActivity activity = getActivity(); + if (activity != null) { + FragmentManager fragmentManager = activity.getSupportFragmentManager(); + if (!fragmentManager.isStateSaved()) { + fragmentManager.popBackStack(); + } + } } } diff --git a/OsmAnd/src/net/osmand/plus/base/ContextMenuFragment.java b/OsmAnd/src/net/osmand/plus/base/ContextMenuFragment.java index 64b0d8b4a0..1e0b10ee31 100644 --- a/OsmAnd/src/net/osmand/plus/base/ContextMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/base/ContextMenuFragment.java @@ -937,11 +937,9 @@ public abstract class ContextMenuFragment extends BaseOsmAndFragment { if (isSingleFragment()) { FragmentActivity activity = getActivity(); if (activity != null) { - try { - activity.getSupportFragmentManager().popBackStack(getFragmentTag(), - FragmentManager.POP_BACK_STACK_INCLUSIVE); - } catch (Exception e) { - // + FragmentManager fragmentManager = activity.getSupportFragmentManager(); + if (!fragmentManager.isStateSaved()) { + fragmentManager.popBackStack(getFragmentTag(), FragmentManager.POP_BACK_STACK_INCLUSIVE); } } } diff --git a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java index 4411ef4551..36df3fdb64 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java @@ -1,7 +1,6 @@ package net.osmand.plus.helpers; import android.annotation.SuppressLint; -import android.app.Activity; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; @@ -341,9 +340,10 @@ public class ImportHelper { @Override protected void onPreExecute() { - progress = ProgressDialog - .show(activity, app.getString(R.string.loading_smth, ""), - app.getString(R.string.loading_data)); + if (AndroidUtils.isActivityNotDestroyed(activity)) { + progress = ProgressDialog.show(activity, app.getString(R.string.loading_smth, ""), + app.getString(R.string.loading_data)); + } } @Override @@ -362,7 +362,7 @@ public class ImportHelper { @Override protected void onPostExecute(GPXFile result) { - if (AndroidUtils.isActivityNotDestroyed(activity)) { + if (progress != null && AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); } Toast.makeText(activity, R.string.fav_imported_sucessfully, Toast.LENGTH_LONG) @@ -679,7 +679,9 @@ public class ImportHelper { @Override protected void onPreExecute() { - progress = ProgressDialog.show(activity, app.getString(R.string.loading_smth, ""), app.getString(R.string.loading_data)); + if (AndroidUtils.isActivityNotDestroyed(activity)) { + progress = ProgressDialog.show(activity, app.getString(R.string.loading_smth, ""), app.getString(R.string.loading_data)); + } mFileName = fileName; } @@ -705,7 +707,7 @@ public class ImportHelper { loadRoutingFiles(app, new AppInitializer.LoadRoutingFilesCallback() { @Override public void onRoutingFilesLoaded() { - if (AndroidUtils.isActivityNotDestroyed(activity)) { + if (progress != null && AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); } RoutingConfiguration.Builder builder = app.getCustomRoutingConfig(mFileName); @@ -720,7 +722,7 @@ public class ImportHelper { } }); } else { - if (AndroidUtils.isActivityNotDestroyed(activity)) { + if (progress != null && AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); } app.showShortToastMessage(app.getString(R.string.file_import_error, mFileName, error)); @@ -762,7 +764,9 @@ public class ImportHelper { @Override protected void onPreExecute() { - progress = ProgressDialog.show(activity, app.getString(R.string.loading_smth, ""), app.getString(R.string.loading_data)); + if (AndroidUtils.isActivityNotDestroyed(activity)) { + progress = ProgressDialog.show(activity, app.getString(R.string.loading_smth, ""), app.getString(R.string.loading_data)); + } } @Override @@ -783,7 +787,7 @@ public class ImportHelper { app.getSettingsHelper().importSettings(file, latestChanges, version, new SettingsImportListener() { @Override public void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List items) { - if (AndroidUtils.isActivityNotDestroyed(activity)) { + if (progress != null && AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); } if (succeed) { @@ -797,7 +801,7 @@ public class ImportHelper { } }); } else { - if (AndroidUtils.isActivityNotDestroyed(activity)) { + if (progress != null && AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); } app.showShortToastMessage(app.getString(R.string.file_import_error, name, error)); @@ -889,7 +893,9 @@ public class ImportHelper { @Override protected void onPreExecute() { - progress = ProgressDialog.show(activity, app.getString(R.string.loading_smth, ""), app.getString(R.string.loading_data)); + if (AndroidUtils.isActivityNotDestroyed(activity)) { + progress = ProgressDialog.show(activity, app.getString(R.string.loading_smth, ""), app.getString(R.string.loading_data)); + } mFileName = fileName; } @@ -916,7 +922,7 @@ public class ImportHelper { } else { app.showShortToastMessage(app.getString(R.string.file_import_error, mFileName, error)); } - if (AndroidUtils.isActivityNotDestroyed(activity)) { + if (progress != null && AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java index 5153698081..dbb14182d7 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java @@ -2120,9 +2120,9 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo public void dismissMenu() { FragmentActivity activity = getActivity(); if (activity != null) { - try { - activity.getSupportFragmentManager().popBackStack(TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE); - } catch (Exception e) { + FragmentManager fragmentManager = activity.getSupportFragmentManager(); + if (!fragmentManager.isStateSaved()) { + fragmentManager.popBackStack(TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE); } } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/dialogs/ContextMenuCardDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/dialogs/ContextMenuCardDialogFragment.java index 8b2934241b..541bbef145 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/dialogs/ContextMenuCardDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/dialogs/ContextMenuCardDialogFragment.java @@ -134,11 +134,9 @@ public class ContextMenuCardDialogFragment extends BaseOsmAndFragment { public void dismiss() { MapActivity activity = dialog.getMapActivity(); if (activity != null) { - try { - activity.getSupportFragmentManager().popBackStack(TAG, - FragmentManager.POP_BACK_STACK_INCLUSIVE); - } catch (Exception e) { - e.printStackTrace(); + FragmentManager fragmentManager = activity.getSupportFragmentManager(); + if (!fragmentManager.isStateSaved()) { + fragmentManager.popBackStack(TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE); } } } diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java index 68544590f4..9d25e244cd 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java @@ -1023,7 +1023,10 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC hideToolbar(); mapActivity.updateStatusBarColor(); mapActivity.refreshMap(); - getChildFragmentManager().popBackStack(); + FragmentManager fragmentManager = getChildFragmentManager(); + if (!fragmentManager.isStateSaved()) { + fragmentManager.popBackStack(); + } } super.onDismiss(dialog); } diff --git a/OsmAnd/src/net/osmand/plus/settings/BaseSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/BaseSettingsFragment.java index 27758dc6e2..f07bf164ec 100644 --- a/OsmAnd/src/net/osmand/plus/settings/BaseSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/BaseSettingsFragment.java @@ -585,7 +585,10 @@ public abstract class BaseSettingsFragment extends PreferenceFragmentCompat impl public void dismiss() { FragmentActivity activity = getActivity(); if (activity != null) { - activity.getSupportFragmentManager().popBackStack(); + FragmentManager fragmentManager = activity.getSupportFragmentManager(); + if (!fragmentManager.isStateSaved()) { + fragmentManager.popBackStack(); + } } }