question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OnDetach will not be called on page reload

See original GitHub issue

Description of the bug

The documentation states

The onAttach() method is invoked when the Component has attached to the UI. Its counterpart, the onDetach() method, is invoked right before the component detaches from the UI. These are good points at which to reserve and release resources used by the component.

This is what I did. Sadly, the onDetach() method is not called if a user decides to reload the page. Thus, all resources acquired during onAttach() leak.

Expected behavior

onDetach() will always be called if the component is detached - regardless the reason.

Minimal reproducible example

public class TestClass extends Component {

   @Override
   protected void onAttach(AttachEvent attachEvent) {
      super.onAttach(attachEvent);
      System.out.println("Calling onAttach");
   }

   @Override
   protected void onDetach(DetachEvent detachEvent) {
      super.onDetach(detachEvent);
      System.out.println("Calling onDetach");
   }
}

Versions

  • Vaadin / Flow version:
  • Java version: kotlin with JDK 17
  • OS version: Arch Linux (current)
  • Browser version (if applicable): Firefox 97

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Artur-commented, Apr 20, 2022

To be clear, onDetach is always called and the API contract is not broken. It is called way later than you would expect though (when the session expires).

No matter if we call this enhancement or bug or anything else, I think it should be improved (fixed). For instance by integrating the mentioned cookbook recipe.

1reaction
tarekorabycommented, Apr 19, 2022

There is a workaround based on Beacon API documented at https://cookbook.vaadin.com/notice-closed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Don't call onDisconnect() on page refresh in Firebase
Since onDisconnect() is a server side operation, and the connection to the server is severed any time a page is unloaded, the simple...
Read more >
Fragment | Android Developers
A Fragment is closely tied to the Activity it is in, and can not be used apart from ... onDetach() called immediately prior...
Read more >
chrome.tabs - Chrome Developers
Permissions. Most features do not require any permissions to use. For example: creating a new tab, reloading a tab, navigating to another URL,...
Read more >
5 common mistakes when using Architecture Components
Fragments have tricky lifecycle and when a fragment gets detached and ... so onDestroy() is not called and DESTROYED state is not reached....
Read more >
Handling Orientation Changes on Android - Medium
Android does this so that your application can reload resources based on the new ... Notice that Android does not call onCreate and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found