0

If you change the View multiple times sometimes a new View is getting created in the Viewfactory. (I'm using Afterburner btw, but that shouldn't be the Issue) This happens on every device (Desktop and Mobile)

The following code is in the init method

addViewFactory(viewname, () -> {
    return new ExampleView();
})

The above example produces multiple instances of the same view (which breaks some presenters of mine)

A quick fix can be seen below, but shouldn't be necessary.

ExampleView view = null;
addViewFactory(viewname, () -> {
    if (view == null) {
        view = (View) new ExampleView.getView();
    }else{
        //comment out the line below to see that this is really happening
        //throw new RuntimeException("Created View multiple times");
    }
    return view;
});

EDIT:
The View-Changing happens still with switchView(String)

LucaZ
  • 87
  • 8

1 Answers1

0

If you check any of the Gluon mobile samples, or if you create a new project with the Gluon plugin, you will notice that the addViewFactory calls are done just once, on the init() method, which is called once, before the Application.start() method.

@Override
public void init() {
    addViewFactory(PRIMARY_VIEW, () -> new PrimaryView(PRIMARY_VIEW));
    addViewFactory(SECONDARY_VIEW, () -> new SecondaryView(SECONDARY_VIEW));
}    

Under the hood, when you call addViewFactory, the view instance is cached in a map of views. This guarantee that you can't add more than once the same view, and you can get the instance of a given view at any time.

So you don't need to create a View every time you switch from a previous one, just call:

MobileApplication.getInstance().switchView(SECONDARY_VIEW);
José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • I still switch the view via this method and the addViewFactory call is in the init method. What I was saying is that when I switch Views with switchView(String), then the supplier gets called multiple times even though I only registered it once. – LucaZ Apr 14 '16 at 12:57
  • I can't reproduce your issue. Just create a new multiple view project with the Gluon Plugin, run it and you'll see views are created just once. Otherwise post relevant code so we can reproduce the issue. – José Pereda Apr 14 '16 at 13:11