Fix several possible crashes with fragment manager and import dialogs

This commit is contained in:
Vitaliy 2020-03-12 12:15:45 +02:00
parent 6f8d043612
commit 63e2ba3493
9 changed files with 56 additions and 39 deletions

View file

@ -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);
}

View file

@ -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();

View file

@ -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();
}
}
}
}

View file

@ -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);
}
}
}

View file

@ -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<SettingsHelper.SettingsItem> 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();
}
}

View file

@ -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);
}
}
}

View file

@ -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);
}
}
}

View file

@ -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);
}

View file

@ -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();
}
}
}