Fix crashes on screen orientation change. Resetting listeners to fragments.

This commit is contained in:
madwasp79 2019-05-15 14:59:47 +03:00
parent 58601045e1
commit 7761039120
6 changed files with 173 additions and 59 deletions

View file

@ -1,6 +1,5 @@
package net.osmand.plus.profiles;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
@ -8,15 +7,14 @@ import android.support.v4.view.MenuItemCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import net.osmand.PlatformUtil;
import net.osmand.plus.R;
import net.osmand.plus.activities.OsmandActionBarActivity;
public class EditProfileActivity extends OsmandActionBarActivity {
public static final int DELETE_ID = 1010;
public static final String FRAGMENT_TAG = "editProfileFragment";
public static final String EDIT_PROFILE_FRAGMENT_TAG = "editProfileFragment";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
@ -28,7 +26,7 @@ public class EditProfileActivity extends OsmandActionBarActivity {
EditProfileFragment editProfileFragment = new EditProfileFragment();
editProfileFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(android.R.id.content,
editProfileFragment, FRAGMENT_TAG).commit();
editProfileFragment, EDIT_PROFILE_FRAGMENT_TAG).commit();
}
}
@ -49,7 +47,8 @@ public class EditProfileActivity extends OsmandActionBarActivity {
finish();
return true;
case DELETE_ID:
((EditProfileFragment)getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG)).onDeleteProfileClick();
((EditProfileFragment)getSupportFragmentManager().findFragmentByTag(
EDIT_PROFILE_FRAGMENT_TAG)).onDeleteProfileClick();
return true;
}

View file

@ -71,8 +71,9 @@ public class EditProfileFragment extends BaseOsmAndFragment {
private boolean isUserProfile = false;
private boolean isDataChanged = false;
private ProfileTypeDialogListener profileTypeDialogListener = null;
private ProfileTypeDialogListener navTypeListener = null;
private IconIdListener iconIdListener = null;
private ProfileTypeDialogListener baseTypeListener = null;
private ImageView profileIcon;
private LinearLayout profileIconBtn;
@ -96,6 +97,8 @@ public class EditProfileFragment extends BaseOsmAndFragment {
private Button cancelBtnSV;
private Button saveButtonSV;
boolean isNightMode;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -108,6 +111,7 @@ public class EditProfileFragment extends BaseOsmAndFragment {
profile = new TempApplicationProfile(mode, isNew, isUserProfile);
}
isNightMode = !app.getSettings().isLightContent();
routingProfiles = getRoutingProfiles();
}
@ -115,7 +119,7 @@ public class EditProfileFragment extends BaseOsmAndFragment {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
final boolean isNightMode = !app.getSettings().isLightContent();
final View view = inflater.inflate(R.layout.fragment_selected_profile, container, false);
@ -194,16 +198,7 @@ public class EditProfileFragment extends BaseOsmAndFragment {
public void onClick(View v) {
if (isUserProfile || isNew) {
final ProfileBottomSheetDialogFragment dialog = new ProfileBottomSheetDialogFragment();
dialog.setProfileTypeListener(new ProfileTypeDialogListener() {
@Override
public void onSelectedType(int pos) {
String key = SettingsProfileFragment.getBaseProfiles(getMyApplication())
.get(pos).getStringKey();
setupBaseProfileView(key);
profile.parent = ApplicationMode
.valueOfStringKey(key, ApplicationMode.DEFAULT);
}
});
dialog.setProfileTypeListener(baseTypeListener);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(TYPE_APP_PROFILE,
SettingsProfileFragment.getBaseProfiles(getMyApplication()));
@ -268,36 +263,6 @@ public class EditProfileFragment extends BaseOsmAndFragment {
profileIcon.setImageDrawable(app.getUIUtilities().getIcon(startIconId, iconColor));
profileTypeDialogListener = new ProfileTypeDialogListener() {
@Override
public void onSelectedType(int pos) {
isDataChanged = true;
for (int i = 0; i < routingProfiles.size(); i++) {
if (i == pos) {
routingProfiles.get(i).setSelected(true);
} else {
routingProfiles.get(i).setSelected(false);
}
}
selectedRoutingProfile = routingProfiles.get(pos);
navTypeEt.setText(selectedRoutingProfile.getName());
LOG.debug(routingProfiles);
profile.setRoutingProfile(selectedRoutingProfile);
}
};
iconIdListener = new IconIdListener() {
@Override
public void selectedIconId(int iconRes) {
isDataChanged = true;
profile.setIconId(iconRes);
profileIcon.setImageDrawable(app.getUIUtilities().getIcon(iconRes,
isNightMode ? R.color.active_buttons_and_links_dark
: R.color.active_buttons_and_links_light));
profile.setIconId(iconRes);
}
};
profileNameEt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
@ -325,8 +290,8 @@ public class EditProfileFragment extends BaseOsmAndFragment {
@Override
public void onClick(View v) {
if (isNew || isUserProfile) {
final ProfileBottomSheetDialogFragment fragment = new ProfileBottomSheetDialogFragment();
fragment.setProfileTypeListener(profileTypeDialogListener);
ProfileBottomSheetDialogFragment fragment = new ProfileBottomSheetDialogFragment();
fragment.setProfileTypeListener(navTypeListener);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("routing_profiles", routingProfiles);
fragment.setArguments(bundle);
@ -468,10 +433,89 @@ public class EditProfileFragment extends BaseOsmAndFragment {
}
}
});
return view;
}
@Override
public void onResume() {
LOG.debug("onResume - EditProfileFragment");
baseTypeListener = new ProfileTypeDialogListener() {
@Override
public void onSelectedType(int pos) {
String key = SettingsProfileFragment.getBaseProfiles(getMyApplication())
.get(pos).getStringKey();
setupBaseProfileView(key);
profile.parent = ApplicationMode
.valueOfStringKey(key, ApplicationMode.DEFAULT);
}
};
navTypeListener = new ProfileTypeDialogListener() {
@Override
public void onSelectedType(int pos) {
updateRoutingProfile(pos);
}
};
iconIdListener = new IconIdListener() {
@Override
public void selectedIconId(int iconRes) {
isDataChanged = true;
profile.setIconId(iconRes);
profileIcon.setImageDrawable(app.getUIUtilities().getIcon(iconRes,
isNightMode ? R.color.active_buttons_and_links_dark
: R.color.active_buttons_and_links_light));
profile.setIconId(iconRes);
}
};
super.onResume();
}
IconIdListener getIconListener() {
if (iconIdListener == null) {
iconIdListener = new IconIdListener() {
@Override
public void selectedIconId(int iconRes) {
isDataChanged = true;
profile.setIconId(iconRes);
profileIcon.setImageDrawable(app.getUIUtilities().getIcon(iconRes,
isNightMode ? R.color.active_buttons_and_links_dark
: R.color.active_buttons_and_links_light));
profile.setIconId(iconRes);
}
};
}
return iconIdListener;
}
ProfileTypeDialogListener getBaseProfileListener() {
if (baseTypeListener == null) {
baseTypeListener = new ProfileTypeDialogListener() {
@Override
public void onSelectedType(int pos) {
String key = SettingsProfileFragment.getBaseProfiles(getMyApplication())
.get(pos).getStringKey();
setupBaseProfileView(key);
profile.parent = ApplicationMode
.valueOfStringKey(key, ApplicationMode.DEFAULT);
}
};
}
return baseTypeListener;
}
ProfileTypeDialogListener getNavProfileListener() {
if (navTypeListener == null) {
navTypeListener = new ProfileTypeDialogListener() {
@Override
public void onSelectedType(int pos) {
updateRoutingProfile(pos);}
};
}
return navTypeListener;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
@ -489,6 +533,20 @@ public class EditProfileFragment extends BaseOsmAndFragment {
}
void updateRoutingProfile(int pos) {
isDataChanged = true;
for (int i = 0; i < routingProfiles.size(); i++) {
if (i == pos) {
routingProfiles.get(i).setSelected(true);
} else {
routingProfiles.get(i).setSelected(false);
}
}
selectedRoutingProfile = routingProfiles.get(pos);
navTypeEt.setText(selectedRoutingProfile.getName());
profile.setRoutingProfile(selectedRoutingProfile);
}
void activateMode(ApplicationMode mode) {
if (!ApplicationMode.values(app).contains(mode)) {
StringBuilder s = new StringBuilder(ApplicationMode.DEFAULT.getStringKey() + ",");
@ -541,8 +599,6 @@ public class EditProfileFragment extends BaseOsmAndFragment {
return false;
}
//check for duplicates
for (ApplicationMode m : ApplicationMode.allPossibleValues()) {
if (m.getUserProfileName() != null && getActivity() != null) {
if (m.getUserProfileName().equals(profile.getUserProfileTitle())) {
@ -707,7 +763,6 @@ public class EditProfileFragment extends BaseOsmAndFragment {
return profilesObjects;
}
private class TempApplicationProfile {
int key = -1;

View file

@ -56,7 +56,6 @@ public class ProfileBottomSheetDialogFragment extends BottomSheetDialogFragment
profiles = args.getParcelableArrayList(TYPE_APP_PROFILE);
type = TYPE_APP_PROFILE;
} else {
//todo notify on empty list;
dismiss();
}
@ -88,7 +87,11 @@ public class ProfileBottomSheetDialogFragment extends BottomSheetDialogFragment
listListener = new ProfileTypeDialogListener() {
@Override
public void onSelectedType(int pos) {
if (listener == null) {
resetListener();
}
listener.onSelectedType(pos);
dismiss();
}
};
@ -109,8 +112,9 @@ public class ProfileBottomSheetDialogFragment extends BottomSheetDialogFragment
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
public void onResume() {
super.onResume();
LOG.debug("onResume - ProfileBottomSheetDialogFragment");
}
private static boolean isNightMode(OsmandApplication ctx) {
@ -154,7 +158,8 @@ public class ProfileBottomSheetDialogFragment extends BottomSheetDialogFragment
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onSelectedType(position);
listener.onSelectedType(position);
holder.radioButton.setChecked(true);
if (item instanceof RoutingProfile) {
@ -193,6 +198,22 @@ public class ProfileBottomSheetDialogFragment extends BottomSheetDialogFragment
}
}
void resetListener() {
if (getActivity() != null && getActivity() instanceof EditProfileActivity) {
EditProfileFragment f = (EditProfileFragment) getActivity().getSupportFragmentManager()
.findFragmentByTag(EditProfileActivity.EDIT_PROFILE_FRAGMENT_TAG);
if (type.equals(TYPE_APP_PROFILE)) {
listener = f.getBaseProfileListener();
} else if (type.equals(TYPE_NAV_PROFILE)) {
listener = f.getNavProfileListener();
}
} else if (getActivity() != null && getActivity() instanceof SettingsProfileActivity) {
SettingsProfileFragment f = (SettingsProfileFragment) getActivity().getSupportFragmentManager()
.findFragmentByTag(SettingsProfileActivity.SETTINGS_PROFILE_FRAGMENT_TAG);
listener = f.getBaseProfileListener();
}
}
class ItemViewHolder extends RecyclerView.ViewHolder {
TextView title, descr;
RadioButton radioButton;

View file

@ -72,6 +72,13 @@ public class SelectIconBottomSheetDialogFragment extends BottomSheetDialogFragme
listListener = new IconIdListener() {
@Override
public void selectedIconId(int iconRes) {
if (listener == null) {
if (getActivity() != null && getActivity() instanceof EditProfileActivity) {
EditProfileFragment f = (EditProfileFragment) getActivity().getSupportFragmentManager()
.findFragmentByTag(EditProfileActivity.EDIT_PROFILE_FRAGMENT_TAG);
listener = f.getIconListener();
}
}
listener.selectedIconId(iconRes);
dismiss();
}

View file

@ -7,6 +7,8 @@ import net.osmand.plus.activities.OsmandActionBarActivity;
public class SettingsProfileActivity extends OsmandActionBarActivity {
public static final String SETTINGS_PROFILE_FRAGMENT_TAG = "settingsProfileFragment";
@Override
public void onCreate(Bundle savedInstanceState) {
getMyApplication().applyTheme(this);
@ -16,7 +18,7 @@ public class SettingsProfileActivity extends OsmandActionBarActivity {
if (savedInstanceState == null) {
SettingsProfileFragment profileFragment = new SettingsProfileFragment();
profileFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(android.R.id.content, profileFragment).commit();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, profileFragment, SETTINGS_PROFILE_FRAGMENT_TAG).commit();
}
}

View file

@ -140,12 +140,42 @@ public class SettingsProfileFragment extends BaseOsmAndFragment {
@Override
public void onResume() {
super.onResume();
typeListener = new ProfileTypeDialogListener() {
@Override
public void onSelectedType(int pos) {
LOG.debug("Base profile: " + baseProfiles.get(pos).getName());
Intent intent = new Intent(getActivity(), EditProfileActivity.class);
intent.putExtra("isNew", true);
intent.putExtra("isUserProfile", true);
intent.putExtra("stringKey", baseProfiles.get(pos).getStringKey());
startActivity(intent);
}
};
allAppModes = ApplicationMode.allPossibleValues();
allAppModes.remove(ApplicationMode.DEFAULT);
adapter.updateItemsList(allAppModes, new LinkedHashSet<>(ApplicationMode.values(getMyApplication())));
}
ProfileTypeDialogListener getBaseProfileListener() {
if (typeListener == null) {
typeListener = new ProfileTypeDialogListener() {
@Override
public void onSelectedType(int pos) {
LOG.debug("Base profile: " + baseProfiles.get(pos).getName());
Intent intent = new Intent(getActivity(), EditProfileActivity.class);
intent.putExtra("isNew", true);
intent.putExtra("isUserProfile", true);
intent.putExtra("stringKey", baseProfiles.get(pos).getStringKey());
startActivity(intent);
}
};
}
return typeListener;
}
static ArrayList<BaseProfile> getBaseProfiles(Context ctx) {
ArrayList<BaseProfile> profiles = new ArrayList<>();
for (ApplicationMode mode : ApplicationMode.getDefaultValues()) {