Is it a good practice to use Realm with try-with-resources?

Viewed 832

In many places there are recommendations to call Realm.getDefaultInstance() in onCreate method of the Activity and call close on Realm instance in onDestroy (or in corresponding methods of presenter).

However, for me it would be cleaner to use Java's try-with-resources construct:

try (final Realm realm = Realm.getDefaultInstance()) {
  // do stuff 
}

Why cleaner? IMO it is easier to manage that narrow scope of realm instance. Getting the instance in one moment of lifecycle and closing it in the another one, reminds me of old days with C++, when we had to worry about calling delete in right moment.

The question is: is it a bad practice to use Realm in such way? Why none of tutorials mention it?

1 Answers
Related