1

In my JavaFX application, at a specific page I have to initialize (when loading the page) some devices such as iris scanner, fingerprint scanner, cameras etc. Before leaving the page/controller it is necessary to deinitialize the devices. There are two buttons in that page. If the user leaves this page by clicking one of these two buttons I can easily deinitialize them. But if the user leaves the page by clicking any menu/sub menu (there are more than 30 menu and sub menu in this application), how can I deinitialize these devices?

There is no destructor in java and I also tried using finalize but nothing comes out.

  • [mcve] please .. don't understand where exactly you see a problem: are you just not inclined to type those x deinitialize commands or do you see a deeper issue? – kleopatra Sep 16 '19 at 11:37
  • You could add a listener to the [parent property](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/Node.html#parentProperty--) of the page which contains the controls for the devices you wish to de-initialize, and do the work to de-initialize the devices if the parent changes to `null`. – jewelsea Sep 16 '19 at 22:55
  • I had similar issues, but in my case it was a static reference deep inside causing it . – Harsh May 19 '22 at 12:41

1 Answers1

1

You cannot rely only on the finalize() method. An answer to this request seems to show that this method is used by the garbage collector to check data loss references. So you need to destroy objects by-hand in a custom method, then finalize() will be automatically applied.

You must create a deinitialize() method (public or package-private, depends on the location of your MenuBar controller) then call it inside your sub-menu item.

0009laH
  • 1,960
  • 13
  • 27
  • There are more than 30 sub menu and I have to call deinitialize() from all sub menu. I think it isn't a better solution. I should looking forward to get the better solution. – Iqbal Hossain Sep 16 '19 at 08:03
  • I didn't understand :) You want de-initialization to be done without explicitely calling a method in your actionners, because it would be too much to write on your code. – 0009laH Sep 16 '19 at 08:06
  • De-initialization will be done explicitely but I don't want to call de-initialization from 30 sub menu. I just want a short cut way. – Iqbal Hossain Sep 16 '19 at 08:33