0

I want to prevent the user that he will loose his changes in an EditView when changing the view to another.

I use MVP4G in my project and the project is divided as mvp's structure (one package for the template another one for views ..) is there any solution to detect the EditView in the eventBus. or detect the current View shown to user

Thanks in advance

essalprod
  • 28
  • 1
  • 8

2 Answers2

0

Thanks to the Navigation Event feature in mvp4g, the presenter will get control before the view changes. At this point the presenter can decide if the navigation will be done or not. This is the correct place in a mvp4g application to save your data.

First zu have to mark all events in the eventbus that will change your view with:

@Event(..., navigationEvent = true)
void goToPage1();

Next your presenters have to implement the NavigationConfirmationInterface and the requires confirm-method:

public class Presenter extends ... implements NavigationConfirmationInterface {
     public void confirm(NavigationEventCommand event) {
          //pseudo method to verify if the view has changed
          if (isViewModified(){
               //Window shouldn't be used inside a presenter
               //this is just to give a simple example
               if (Window.confirm("Are you sure you want to leave?")){
                    event.fireEvent();
               }                                
          } else {
               event.fireEvent();
          }
     }
}

And the last thing to do, is to set the presenter of the current view to the confirmation presenter by calling:

event.fireEvent(false);

This is usually done when the presenter gets control.

You will find the documentation here:

https://github.com/FrankHossfeld/mvp4g/wiki/03.-Defining-EventBus#navigation-event

El Hoss
  • 3,767
  • 2
  • 18
  • 24
  • It doesn't work my structure is : I have template package that contains : MenuOneView/MenuOnePresenter MenuTwoView/MenuTwoPresenter and in the MVP's package : AView /APresenter SView/SPresenter MView/MPresenter DView/DPresenter the two EditView are : AView, MView What I did to test : I've altered these methods in the EventBus onGoToA(navigationEvent=true) onGoToM(navigationEvent=true) In the APresenter and MPresenter I've added implements NavigationConfirmationInterface in the MenuTwoPresenter I've implemented too void confirm Is there any example? Thank's a lot – essalprod May 29 '15 at 22:34
  • can you provide a small sample project and post it in the mvp4g group? https://groups.google.com/forum/#!forum/mvp4g – El Hoss May 30 '15 at 08:32
0

Thanks to MVP4G's team including El Hoss who gives me a hint to check the MVP4G's blog.. I've solved my problem by following this example http://mvp4g.blogspot.com/2011/06/navigation-control.html

essalprod
  • 28
  • 1
  • 8