Avoiding IllegalStateException in MVP when view is not ready yet

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật