Fixed bug when application shows wrong layout after rotation

This commit is contained in:
Denis 2014-08-08 16:29:02 +03:00
parent 3fcfa5f2aa
commit bde478e39a
2 changed files with 71 additions and 56 deletions

View file

@ -92,9 +92,9 @@ public class SherpafyTourFragment extends SherlockListFragment {
if (si.type == StageItemType.GALLERY) { if (si.type == StageItemType.GALLERY) {
// ((TourViewActivity) getSherlockActivity()).showGallery(tour); // ((TourViewActivity) getSherlockActivity()).showGallery(tour);
} else if (si.type == StageItemType.OVERVIEW) { } else if (si.type == StageItemType.OVERVIEW) {
((TourViewActivity) getSherlockActivity()).showHtmlFragment(si.header, tour.getFulldescription()); ((TourViewActivity) getSherlockActivity()).showDetailedOverview(si.header, tour.getFulldescription());
} else if (si.type == StageItemType.INSTRUCTIONS) { } else if (si.type == StageItemType.INSTRUCTIONS) {
((TourViewActivity) getSherlockActivity()).showHtmlFragment(si.header, tour.getInstructions()); ((TourViewActivity) getSherlockActivity()).showDetailedInstructions(si.header, tour.getInstructions());
} }
} }
} }

View file

@ -48,12 +48,14 @@ public class TourViewActivity extends SherlockFragmentActivity {
private static final int STATE_TOUR_VIEW = 2; private static final int STATE_TOUR_VIEW = 2;
private static final int STATE_STAGE_OVERVIEW = 3; private static final int STATE_STAGE_OVERVIEW = 3;
private static final int STATE_DETAILED_OVERVIEW = 4; private static final int STATE_DETAILED_OVERVIEW = 4;
private static final int STATE_DETAILED_INSTRUCTIONS = 5;
private static final int STAGE_GALLERY = 6;
private static int state = STATE_LOADING; private static int state = STATE_LOADING;
public static final int APP_EXIT_CODE = 4; public static final int APP_EXIT_CODE = 4;
public static final String APP_EXIT_KEY = "APP_EXIT_KEY"; public static final String APP_EXIT_KEY = "APP_EXIT_KEY";
private SherpafyCustomization customization; private SherpafyCustomization customization;
private Point displaySize; private Point displaySize;
private ActionBarDrawerToggle mDrawerToggle; private ActionBarDrawerToggle mDrawerToggle;
@ -112,6 +114,12 @@ public class TourViewActivity extends SherlockFragmentActivity {
if (getMyApplication().isApplicationInitializing()) { if (getMyApplication().isApplicationInitializing()) {
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, new SherpafyLoadingFragment()).commit(); fragmentManager.beginTransaction().replace(R.id.content_frame, new SherpafyLoadingFragment()).commit();
} else if (state == STATE_DETAILED_INSTRUCTIONS) {
TourInformation tour = (TourInformation) selectedItem;
showDetailedInstructions(getString(R.string.sherpafy_instructions), tour.getInstructions());
} else if (state == STATE_DETAILED_OVERVIEW) {
TourInformation tour = (TourInformation) selectedItem;
showDetailedInstructions(getString(R.string.sherpafy_overview), tour.getFulldescription());
} else { } else {
showSelectedItem(); showSelectedItem();
} }
@ -122,50 +130,47 @@ public class TourViewActivity extends SherlockFragmentActivity {
if (state == STATE_SELECT_TOUR) { if (state == STATE_SELECT_TOUR) {
super.onBackPressed(); super.onBackPressed();
} else if (state == STATE_TOUR_VIEW) { } else if (state == STATE_TOUR_VIEW) {
SherpafyHtmlFragment fragment = (SherpafyHtmlFragment) getSupportFragmentManager().findFragmentByTag(String.valueOf(STATE_DETAILED_OVERVIEW)); selectMenu(R.string.sherpafy_tours);
if (fragment != null && fragment.isVisible()) {
showSelectedItem();
} else {
selectMenu(R.string.sherpafy_tours);
}
} else if (state == STATE_STAGE_OVERVIEW) { } else if (state == STATE_STAGE_OVERVIEW) {
SherpafyStageFragment fragment = (SherpafyStageFragment) getSupportFragmentManager().findFragmentByTag(String.valueOf(state)); SherpafyStageFragment fragment = (SherpafyStageFragment) getSupportFragmentManager().findFragmentByTag(String.valueOf(state));
if (fragment != null) { if (fragment != null) {
fragment.onBackPressed(); fragment.onBackPressed();
} }
} else if (state == STATE_DETAILED_OVERVIEW || state == STATE_DETAILED_INSTRUCTIONS) {
showSelectedItem();
} }
} }
private ArrayAdapter<Object> setupAdapter() { private ArrayAdapter<Object> setupAdapter() {
return new ArrayAdapter<Object>(this, R.layout.sherpafy_drawer_list_item){ return new ArrayAdapter<Object>(this, R.layout.sherpafy_drawer_list_item) {
@Override @Override
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, ViewGroup parent) {
Object it = getItem(position); Object it = getItem(position);
if(convertView == null){ if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.sherpafy_drawer_list_item, null); convertView = getLayoutInflater().inflate(R.layout.sherpafy_drawer_list_item, null);
} }
final ImageView imView = (ImageView) convertView.findViewById(R.id.Icon); final ImageView imView = (ImageView) convertView.findViewById(R.id.Icon);
TextView tv = (TextView) convertView.findViewById(R.id.Text); TextView tv = (TextView) convertView.findViewById(R.id.Text);
if(it.equals(R.string.sherpafy_tours)) { if (it.equals(R.string.sherpafy_tours)) {
imView. imView.
setImageResource(R.drawable.icon_sherpafy); setImageResource(R.drawable.icon_sherpafy);
tv.setText(getString(R.string.sherpafy_tours)); tv.setText(getString(R.string.sherpafy_tours));
} else if(it instanceof TourInformation){ } else if (it instanceof TourInformation) {
if(selectedItem == it) { if (selectedItem == it) {
imView.setImageResource(R.drawable.ic_action_ok_light); imView.setImageResource(R.drawable.ic_action_ok_light);
} else { } else {
imView.setImageResource(R.drawable.ic_action_globus_light); imView.setImageResource(R.drawable.ic_action_globus_light);
} }
tv.setText(((TourInformation) it).getName()); tv.setText(((TourInformation) it).getName());
} else if(it instanceof StageInformation){ } else if (it instanceof StageInformation) {
if(customization.getSelectedStage() == it) { if (customization.getSelectedStage() == it) {
imView.setImageResource(R.drawable.ic_action_gplay_over_light); imView.setImageResource(R.drawable.ic_action_gplay_over_light);
} else if(selectedItem == it) { } else if (selectedItem == it) {
imView.setImageResource(R.drawable.ic_action_ok_light); imView.setImageResource(R.drawable.ic_action_ok_light);
} else { } else {
imView.setImageDrawable( imView.setImageDrawable(
new StageImageDrawable(TourViewActivity.this, StageImageDrawable.MENU_COLOR, new StageImageDrawable(TourViewActivity.this, StageImageDrawable.MENU_COLOR,
(((StageInformation) it).getOrder() + 1)+"", 0)); (((StageInformation) it).getOrder() + 1) + "", 0));
} }
tv.setText(((StageInformation) it).getName()); tv.setText(((StageInformation) it).getName());
} else { } else {
@ -176,19 +181,19 @@ public class TourViewActivity extends SherlockFragmentActivity {
} }
}; };
} }
public void updateActionBarTitle() { public void updateActionBarTitle() {
if(state == STATE_LOADING) { if (state == STATE_LOADING) {
getSupportActionBar().setTitle(R.string.app_name); getSupportActionBar().setTitle(R.string.app_name);
} else if(state == STATE_SELECT_TOUR) { } else if (state == STATE_SELECT_TOUR) {
getSupportActionBar().setTitle(R.string.sherpafy_tours); getSupportActionBar().setTitle(R.string.sherpafy_tours);
} else if(state == STATE_TOUR_VIEW) { } else if (state == STATE_TOUR_VIEW) {
} }
invalidateOptionsMenu(); invalidateOptionsMenu();
} }
@Override @Override
protected void onPostCreate(Bundle savedInstanceState) { protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState); super.onPostCreate(savedInstanceState);
@ -216,7 +221,7 @@ public class TourViewActivity extends SherlockFragmentActivity {
} }
public MenuItem createMenuItem(Menu m, int id, int titleRes, int iconLight, int iconDark, int menuItemType, public MenuItem createMenuItem(Menu m, int id, int titleRes, int iconLight, int iconDark, int menuItemType,
final OnMenuItemClickListener listener) { final OnMenuItemClickListener listener) {
// int r = getMyApplication().getSettings().isLightActionBar() ? iconLight : iconDark; // int r = getMyApplication().getSettings().isLightActionBar() ? iconLight : iconDark;
int r = iconLight; int r = iconLight;
MenuItem menuItem = m.add(0, id, 0, titleRes); MenuItem menuItem = m.add(0, id, 0, titleRes);
@ -254,7 +259,7 @@ public class TourViewActivity extends SherlockFragmentActivity {
} }
state = STATE_SELECT_TOUR; state = STATE_SELECT_TOUR;
setDrawerIndicatorVisible(true); setDrawerIndicatorVisible(true);
} else if(item instanceof TourInformation) { } else if (item instanceof TourInformation) {
state = STATE_TOUR_VIEW; state = STATE_TOUR_VIEW;
if (fragment == null) { if (fragment == null) {
fragment = new SherpafyTourFragment(); fragment = new SherpafyTourFragment();
@ -264,7 +269,7 @@ public class TourViewActivity extends SherlockFragmentActivity {
fragments.put(item, fragment); fragments.put(item, fragment);
} }
setDrawerIndicatorVisible(true); setDrawerIndicatorVisible(true);
} else if(item instanceof StageInformation) { } else if (item instanceof StageInformation) {
state = STATE_STAGE_OVERVIEW; state = STATE_STAGE_OVERVIEW;
if (fragment == null) { if (fragment == null) {
fragment = new SherpafyStageFragment(); fragment = new SherpafyStageFragment();
@ -276,7 +281,7 @@ public class TourViewActivity extends SherlockFragmentActivity {
} }
setDrawerIndicatorVisible(false); setDrawerIndicatorVisible(false);
} }
if(fragment != null) { if (fragment != null) {
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment, String.valueOf(state)).commit(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment, String.valueOf(state)).commit();
} }
selectedItem = item; selectedItem = item;
@ -286,41 +291,41 @@ public class TourViewActivity extends SherlockFragmentActivity {
drawerAdapter.clear(); drawerAdapter.clear();
drawerAdapter.add(R.string.sherpafy_tours); drawerAdapter.add(R.string.sherpafy_tours);
TourInformation selectedTour = customization.getSelectedTour(); TourInformation selectedTour = customization.getSelectedTour();
for(TourInformation it : customization.getTourInformations()) { for (TourInformation it : customization.getTourInformations()) {
int insert = drawerAdapter.getCount(); int insert = drawerAdapter.getCount();
if(it == selectedTour) { if (it == selectedTour) {
insert = 1; insert = 1;
} }
drawerAdapter.insert(it, insert++); drawerAdapter.insert(it, insert++);
if(it == selectedItem || (selectedItem instanceof StageInformation && if (it == selectedItem || (selectedItem instanceof StageInformation &&
((StageInformation) selectedItem).getTour() == it)) { ((StageInformation) selectedItem).getTour() == it)) {
for(StageInformation st : it.getStageInformation()) { for (StageInformation st : it.getStageInformation()) {
drawerAdapter.insert(st, insert++); drawerAdapter.insert(st, insert++);
} }
} else if(it == selectedTour) { } else if (it == selectedTour) {
StageInformation st = customization.getSelectedStage(); StageInformation st = customization.getSelectedStage();
if(st != null) { if (st != null) {
drawerAdapter.insert(st, insert++); drawerAdapter.insert(st, insert++);
} }
} }
} }
updateActionBarTitle(); updateActionBarTitle();
} }
private void setDrawerIndicatorVisible(boolean b) { private void setDrawerIndicatorVisible(boolean b) {
if(mDrawerToggle.isDrawerIndicatorEnabled() != b) { if (mDrawerToggle.isDrawerIndicatorEnabled() != b) {
mDrawerToggle.setDrawerIndicatorEnabled(b); mDrawerToggle.setDrawerIndicatorEnabled(b);
} }
} }
public void showSelectedItem() { public void showSelectedItem() {
if(selectedItem != null) { if (selectedItem != null) {
selectMenu(selectedItem); selectMenu(selectedItem);
} else { } else {
if(customization.getSelectedStage() != null) { if (customization.getSelectedStage() != null) {
selectMenu(customization.getSelectedStage()); selectMenu(customization.getSelectedStage());
} else if(customization.getSelectedTour() != null) { } else if (customization.getSelectedTour() != null) {
selectMenu(customization.getSelectedTour()); selectMenu(customization.getSelectedTour());
} else { } else {
selectMenu(R.string.sherpafy_tours); selectMenu(R.string.sherpafy_tours);
@ -328,7 +333,7 @@ public class TourViewActivity extends SherlockFragmentActivity {
} }
} }
public void showFavoriteFragment(StageInformation stage, StageFavorite sf) { public void showFavoriteFragment(StageInformation stage, StageFavorite sf) {
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentManager fragmentManager = getSupportFragmentManager();
setDrawerIndicatorVisible(false); setDrawerIndicatorVisible(false);
@ -340,8 +345,18 @@ public class TourViewActivity extends SherlockFragmentActivity {
fragment.setArguments(bl); fragment.setArguments(bl);
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
} }
public void showHtmlFragment(String title, String cont) { public void showDetailedInstructions(String title, String cont) {
state = STATE_DETAILED_INSTRUCTIONS;
showHtmlFragment(title, cont);
}
public void showDetailedOverview(String title, String cont) {
state = STATE_DETAILED_OVERVIEW;
showHtmlFragment(title, cont);
}
private void showHtmlFragment(String title, String cont) {
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentManager fragmentManager = getSupportFragmentManager();
setDrawerIndicatorVisible(false); setDrawerIndicatorVisible(false);
SherpafyHtmlFragment fragment = new SherpafyHtmlFragment(); SherpafyHtmlFragment fragment = new SherpafyHtmlFragment();
@ -358,18 +373,18 @@ public class TourViewActivity extends SherlockFragmentActivity {
download.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); download.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(download); startActivity(download);
} }
public void goToMap(LatLon location) { public void goToMap(LatLon location) {
if(location != null) { if (location != null) {
getMyApplication().getSettings().setMapLocationToShow(location.getLatitude(), location.getLongitude(), 16, null); getMyApplication().getSettings().setMapLocationToShow(location.getLatitude(), location.getLongitude(), 16, null);
} }
Intent newIntent = new Intent(this, customization.getMapActivity()); Intent newIntent = new Intent(this, customization.getMapActivity());
newIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); newIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
this.startActivityForResult(newIntent, 0); this.startActivityForResult(newIntent, 0);
} }
public void startStage(final StageInformation stage) { public void startStage(final StageInformation stage) {
if(stage != customization.getSelectedStage() && customization.getSelectedStage() != null) { if (stage != customization.getSelectedStage() && customization.getSelectedStage() != null) {
Builder bld = new AlertDialog.Builder(this); Builder bld = new AlertDialog.Builder(this);
bld.setMessage(R.string.start_new_stage); bld.setMessage(R.string.start_new_stage);
bld.setPositiveButton(R.string.default_buttons_yes, new OnClickListener() { bld.setPositiveButton(R.string.default_buttons_yes, new OnClickListener() {
@ -378,7 +393,7 @@ public class TourViewActivity extends SherlockFragmentActivity {
runStage(stage.getTour(), stage, customization.getSelectedStage() != stage); runStage(stage.getTour(), stage, customization.getSelectedStage() != stage);
} }
}); });
bld.setNegativeButton(R.string.default_buttons_no, null); bld.setNegativeButton(R.string.default_buttons_no, null);
bld.show(); bld.show();
} else { } else {
runStage(stage.getTour(), stage, customization.getSelectedStage() != stage); runStage(stage.getTour(), stage, customization.getSelectedStage() != stage);
@ -387,16 +402,16 @@ public class TourViewActivity extends SherlockFragmentActivity {
public void startTour(final TourInformation tour) { public void startTour(final TourInformation tour) {
if(tour != customization.getSelectedTour() && customization.getSelectedTour() != null) { if (tour != customization.getSelectedTour() && customization.getSelectedTour() != null) {
Builder bld = new AlertDialog.Builder(this); Builder bld = new AlertDialog.Builder(this);
bld.setMessage(R.string.start_new_stage); bld.setMessage(R.string.start_new_stage);
bld.setPositiveButton(R.string.default_buttons_yes, new OnClickListener() { bld.setPositiveButton(R.string.default_buttons_yes, new OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
startTourImpl(tour); startTourImpl(tour);
} }
}); });
bld.setNegativeButton(R.string.default_buttons_no, null); bld.setNegativeButton(R.string.default_buttons_no, null);
bld.show(); bld.show();
} else { } else {
startTourImpl(tour); startTourImpl(tour);
@ -433,14 +448,14 @@ public class TourViewActivity extends SherlockFragmentActivity {
getMyApplication().getSelectedGpxHelper().setGpxFileToDisplay(gpx); getMyApplication().getSelectedGpxHelper().setGpxFileToDisplay(gpx);
} }
} }
} }
WptPt lp = gpx.getLastPoint(); WptPt lp = gpx.getLastPoint();
if(lp != null) { if (lp != null) {
TargetPointsHelper targetPointsHelper = getMyApplication().getTargetPointsHelper(); TargetPointsHelper targetPointsHelper = getMyApplication().getTargetPointsHelper();
targetPointsHelper.navigateToPoint(new LatLon(lp.lat, lp.lon), true, -1, lp.name); targetPointsHelper.navigateToPoint(new LatLon(lp.lat, lp.lon), true, -1, lp.name);
getMyApplication().getSettings().navigateDialog(); getMyApplication().getSettings().navigateDialog();
} }
if(startOver && point != null){ if (startOver && point != null) {
goToMap(new LatLon(point.lat, point.lon)); goToMap(new LatLon(point.lat, point.lon));
} else { } else {
goToMap(null); goToMap(null);