0

I have a class called GraphView that extends View and I do some calculations in side this class
The object of this class represents a custom Graph that I display on the screen. This is created in the MainActivity file

I need to save the value of an array inside the extended View class so I want to create a RandomAccessFile object inside the class, I will probably initialize this object inside the constructor of GraphView, but where do I specify the close() function?

The graph will stay on screen as long as the program runs so I am not sure where the close() function should be, as there does not seem to be a destructor in Java.

user13267
  • 6,871
  • 28
  • 80
  • 138

1 Answers1

1

It's better to open and close a file straight away after you're done with reading/writing. Don't keep it open as you cannot tightly control what happens to your view (e.g. other processes can come in front, OS can decide to kill your process, etc).

Szymon
  • 42,577
  • 16
  • 96
  • 114
  • what if I make a member function in GraphView() called close(), whose sole purpose is to close the RandomAccessFile object, and then call this function in the MainActivity file's onDestroy() ? – user13267 Sep 24 '13 at 04:49
  • I am thinking this because the command to save the variable should come from the main program. The main program has another command button, and if I press that button then the GraphView class's variable should be saved. – user13267 Sep 24 '13 at 04:51
  • I think it's better to always close the external resources as soon as possible. You will avoid any potential problems this way. Opening and closing a file doesn't take long so it's not a big deal. – Szymon Sep 24 '13 at 05:02