0

My application has multiple activities, and the first (primary) activity is only closed when the app should terminate. All the other (secondary) activities are shown above this one.

This primary activity initializes a Singleton class, that should run for the whole lifetime of the app. The secondary activities use make use of this Singleton instance.

When the app should close, the primary activity handles the termination of the Singleton instance (some steps are required) and terminates itself as well.

I was doing this on the onDestroy method of the primary activity, but it has happened for the method not to be called, which can happen, as per an activity life cycle.

My question is then how should one correctly handle the termination of a Singleton that requires some steps to be taken before termination.

Using the onPause would not work because if the app closed when a secondary activity was showing, the onPause of the primary activity would not be called again (as it was already hidden).

Thanks!

AmiguelS
  • 805
  • 2
  • 10
  • 28
  • You need to think of all the possible places that would cause your app to termintate. – Android Admirer Feb 10 '17 at 19:28
  • Then dispose of the singleton parts that could be regenerated when that activity launches – Android Admirer Feb 10 '17 at 19:29
  • @OmarAbdelhafiz Hi! Thanks for your quick reply! That is an option, but a rather messy one. I was hoping for something cleaner, if possible... – AmiguelS Feb 10 '17 at 19:31
  • Make sure to use `!= null` in your main singleton dispose method because some items might not be instantiated and eventually cause an exception – Android Admirer Feb 10 '17 at 19:31
  • oh let me think!! – Android Admirer Feb 10 '17 at 19:32
  • check this out http://stackoverflow.com/questions/21040339/how-to-know-when-my-app-has-been-killed – John Bravado Feb 10 '17 at 19:32
  • Using your onDestroy() method is enough. First, null check all singleton instances, if not null set equal to null. The way you have structured your app is a bit confusing, however. If you are going to leave multiple screens open, while using a singleton pattern it would make more sense (and be more resource efficient) to utilize Fragments. But it sounds like you have the correct idea. – portfoliobuilder Feb 10 '17 at 19:34
  • @portfoliobuilder Lets think in activity layers, the primary activity being layer 0. My layer 0 is a homescreen/dashboard. I never close it because the animations used to "overlap" a new activity give the user a better understanding of the flow of the app. This layer level 0 activity opens one (from a choice of multiple) level 1 activity. This activity then contains fragments. – AmiguelS Feb 10 '17 at 19:39

1 Answers1

0

You will not be able to know if your Application's Process is forcefully killed by the system. Following text from documentation tells that there are times when onDestroy() will not be called. There is no such guarantee for it to be called.

From this page of Android Documentation.

void onDestroy () Perform any final cleanup before an activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

Take a look at this question also

If your steps of the Singleton that must be taken are doing a job like to persist something then What you could do is have an observer pattern that notifies your singleton to take those necessary steps whenever corresponding action happens in any activity that requires those Must to do steps to be taken.

Refer to CommonsWare Answer Here also

And also this page of Android documentation is useful to study

Community
  • 1
  • 1
Shoaib Anwar
  • 1,555
  • 12
  • 26
  • ``You will never be able to know if your Application's Process is forcefully killed by the system.`` This situation is exactly what I was trying to handle... – AmiguelS Feb 10 '17 at 19:42
  • Yes this is the exact situation that you can't be aware of when it is going to happen. You can have a preventive measure like approach towards it. When _Android_ kills a _process_ it does so brutally, simply by recovering the space from memory. `onDestroy()` is never guaranteed to be called. – Shoaib Anwar Feb 10 '17 at 19:55
  • onDestroy will be called if you explicitly call finish() yourself. Additionally, in the case of the application closing, it IS guaranteed to be called. onDestroy() is the final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method. – portfoliobuilder Feb 11 '17 at 01:06
  • Its next paragraph from official documentation. Note: Don't count on this method being called as a place for saving data! Those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is implemented to free resources so that a destroyed activity does not leave such things around while the rest of its application is still running. **There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it**, so it should not be used to do things that are intended to remain around or saved. – Shoaib Anwar Feb 11 '17 at 04:08
  • I have specifically mentioned that when _Application_ is killed by the system than you will not be able to know about it. [Visit this correct marked answer to be sure about this same thing](http://stackoverflow.com/questions/19608948/is-ondestroy-not-always-called) – Shoaib Anwar Feb 11 '17 at 04:35