cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Can't instantiate a fragment after Destory it

legatimatteo
Discoverer
0 Kudos
206

Hi, I'm developing a SAPUI5 application and I need to instantiate a fragment (F) within a view (V).

The issue is that the view (V) can be closed by the user. To ensure good performance, I simply destroy it using:

V.destroyDependents();
V.destroyContent();
V.destroy();

However, the user can reopen the same view, which means I need to re-instantiate it, and with it, the fragment. I'm using this code:

Fragment.load({
    name: "webapp.fragment.DocumentaleDialog",
    id: this.createId("dialogDocumentale"),
    controller: this
}).then((oFragment) => {
    that.oDocumentaleDialog = oFragment;
    that.oView.addDependent(that.oDocumentaleDialog);
    openDocumentaleDialog();
});

The problem I'm encountering is a duplicate ID error when I try to instantiate the fragment again.

View Entire Topic
FabioPagoti
Active Contributor
0 Kudos

I suggest to avoid destroying it to avoid that error.

openDocumentaleDialog: function () {
    if (!this.oDocumentaleDialog) {
        Fragment.load({
            name: "webapp.fragment.DocumentaleDialog",
            id: this.createId("dialogDocumentale"),
            controller: this
        }).then((oFragment) => {
            this.oDocumentaleDialog = oFragment;
            this.getView().addDependent(this.oDocumentaleDialog);
            this.oDocumentaleDialog.open();
        });
    } else {
        this.oDocumentaleDialog.open();
    }
}

 Your assumption "To ensure good performance, I simply destroy it" — is partially correct as recreating the fragment from scratch does have a cost. And to be fair, a popup is usually something very minor when it comes to performance and memory overhead.

legatimatteo
Discoverer
0 Kudos
What about destryoing the view? I need to do it because in my webapp there are a lot of views that can be used and I need to destroy them if no longer usefull; users can still use them but that could be very rare. The fragment issue occurs when i destroy the view and then i re-istanziate it