-1

I have developed two views say View1.xml and View2.xml In both I implemented onInit() and onAfterRendering(). I also implemented back button where I simply use router.navTo() method.

When I come back from View2 to View1, view1's onInit/onAfterRendering is not called. Same happens even if I press browser's back button.

Please help!

user12
  • 55
  • 4
  • 11
  • See _[“onBeforeRendering” or “onAfterRendering” is not called every time the view is opened](https://stackoverflow.com/q/55082731/5846045)_ – Boghyon Hoffmann Dec 18 '19 at 12:54

3 Answers3

0

I checked it in a sample project. It seems that the first view is not destroyed, just went to the background. When you initiates the back operation, it will just move the first view to the top, without calling the rendering methods.

What do you want to do in the renderer methods?

  • If you want to make data related changes visible, rerendering is not necessary, as it's done automatically by data binding
  • If you want to update a control, you can .invalidate() or .rerender() a control directly, which will call the proper lifecycle method of the control. It's much better than rerender the whole view.

To force the rerendering, you can invalidate your view using this.getView().invalidate() or this.getView().rerender() in the navigation event handlers. Be careful to call these two methods only if necessary as it affects the performance of your website.

nistv4n
  • 2,515
  • 2
  • 19
  • 21
  • Thanks!! Another doubt, a browser referesh (F5) would clear the sap.ui.core.model? – user12 Jun 11 '16 at 09:44
  • Yes, it clears the model and everything else. You need to fetch the data again - which will be done automatically because the whole page is loaded again (views, controllers are built again). – nistv4n Jun 11 '16 at 16:12
  • I had save some data in core.model that were being used in multiple pages, hence the problem!! – user12 Jun 11 '16 at 19:22
0

onInit is only and always called exactly once, that's how it is defined! Furthermore, instead of relying on onAfterRendering you should listen for the matched events of the routes, see the navigation and routing tutorial of the SDK! You never know exactly WHEN a view gets rerendered, so make sure you understand when it makes sense to us it, i.e. to modify the DOM after the rerendering has happened or if you need to register third party libs somewhere in the DOM...

Nabi
  • 2,536
  • 1
  • 16
  • 18
0

Please try this piece of code in the oninit() as shown below:

onInit: function() {

    this.getView().addEventDelegate({

        onBeforeShow:function(evt)
        {
            alert("Hi U came to init function");
}
})

},

hope this will work. Please check it and let me know if it works. Thanks, Deepak Raj.

Rahul Sharma
  • 9,534
  • 1
  • 15
  • 37