1

In an effort to improve my coding skills I am trying to make use of the MVP pattern in one of my first Android applications. It is going well but I have hit one issue that I am not sure what is the "proper" way to deal with.

I have a View class that contains a Google Map object. It also contains a "spinner" that allows the user to select among a number of modes for centering the map.

  • case 0 is center on a particular user marker
  • case 1 is center on the center of the group of markers

When the user selects one of these modes the presenter is notified and depending on the mode, works with the model to figure out what new center of the map should be and then tells the View to recenter the map.

The problem is that when the device is rotated the whole "system" is restarted and one of the first things that happens is the android system sets the spinner to the state it was in before it was rotated. This triggers the recenter method which tries to recenter the map before it is ready.

My quick fix for this was to check something in the Model class that should only be true if the map is ready, i.e. there are markers that should have been displayed already.

public void centerOnModeSelected(int position) {
    centerOnModeIndex = position;
    if (mMapMarkers.size() > 0 ) recenterMap();

}

What I am planning on doing is this:

In the presenter constructor, I would have a field

private Boolean mapIsReadyForUpdates = false;

and a method

public void setMapIsReadyForUpdates() {
     mapIsReadyForUpdates = true;
}

and then I would have the View call that method after it has added some markers to the map, or whenever I can be sure It is ready to accept the methods that are currently generating the exceptions.

I guess the other option is to hold that state in the view itself and then block the calls to the map object based on the view's state.

My current methodology is to not have the view maintain state, the presenter is maintaining the state.

I am assuming others have dealt with situations in MVP were a view my not be in a state were it can accept calls to particular methods. Can someone help me understand the trade-offs with my 3 solutions, or suggest a better solution?

My "quick fix" works, but seems like a hack. If there are no markers there is no need to recenter the map, but the fact that markers exist does not mean the map is ready to be updated either.

UPDATE

Since the presenter completely controls the view, it knows when the map is ready to be updated, so all I needed to do was to create a field in the presenter (as mentioned above), and then set it to true, once I was sure I created the map in the view. Then check that field before calling any methods that would try to update the map. I guess this makes perfect sense for the MVP pattern.

migrated from codereview.stackexchange.com Aug 1 '14 at 20:51

This question came from our site for peer programmer code reviews.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.