My app has many views, on each view there are several images and icons that consume memory; as I open the view, the memory used increases to a memory leak. I noticed that the views are properly cached, but in this case I would like to limit the cache and remove the views from the cache, how can I do it?
As you have mentioned, the Gluon views are properly cached.
For that, whenever you add a View
using:
MobileApplication.getInstance().addViewFactory(MY_VIEW, () -> new View(new Label("Hi")));
the Gluon Mobile framework manages for you internally a cache of Views and Layers.
Whenever the view is required, it will be created, and cached. The next time you required it, it will be retrieved from the cache, if present, or created again.
When the memory runs low in your device, the existing views can be removed from the cache automatically.
But if you want to do it manually, Gluon MobileApplication
class actually includes a method to unregister a view from the view factory:
MobileApplication.getInstance().removeViewFactory(MY_VIEW);
Be aware that this will remove the instance and the factory itself, so the next time it is required you will have to add it to the factory again.
For that you can use:
if (! MobileApplication.getInstance().isViewPresent(MY_VIEW)) {
MobileApplication.getInstance().addViewFactory(MY_VIEW, () -> new View(new Label("Hi")));
MobileApplication.getInstance().switchView(MY_VIEW);
}