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

Can't instantiate a fragment after Destory it

legatimatteo
Discoverer
0 Kudos
186

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.

Accepted Solutions (0)

Answers (2)

Answers (2)

junwu
SAP Champion
SAP Champion
0 Kudos

anyway you are destroying the view, why not destroy the fragment by your code?

legatimatteo
Discoverer
0 Kudos
I'm destroying the view because it became useless 99% of the time. I'm not destroying the fragment because, reading the docs, I understood that 'destroy view implies destroy fragment.'
junwu
SAP Champion
SAP Champion
0 Kudos
apparently it seems not to be the case, why being so stubborn? just destroy the fragment
legatimatteo
Discoverer
0 Kudos
Despite following your suggestion to destroy the fragment, I'm still facing a unique ID error. I even made sure to destroy all dependents by iterating through V.getDependents() and calling oD.destroy() on each before V.destroy(). While V.getDependents() appears empty after this process, suggesting the destroy function is working, SAP seems to be holding onto these IDs in memory.
junwu
SAP Champion
SAP Champion
0 Kudos
V.getDependents(), if you debug, were you able to find the fragment? another thing is I never had the need to destroy a view. why you have to do that?
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