Leaving an Android application

SDK Version: 
One of the strange things with android is that there is no "the user has closed the application" event. Applications activities often just go to the background, and the android system may kill them whenever it wants, even when another activity of the application is still running in the foreground.

You can not be sure that activities you started and sent to the background are sill there storing their state, and if you are just an android user, you can not be sure that applications that you left simply by pressing back button are completely destroyed.

To make it more complex, using intents it is even possible to start an activity of another application.

You must clearly understand this when you are developing android, otherwise you can make some typical mistakes, and still the application will seem like working... until there is sufficient memory, or a phone call does not interrupt your application.

Usually each activity should open/initialize the resources it needs, even if it is opened from an activity where the same resources were previously acquired, because on low memory the system may kill that activity and free its resources.

(On a level below the activities of course you can track the resource accesses from the activities to do not open and close resources multiple times between activities, but the activity itself must suppose that it needs to be opened at start)

For example a typical error I have seen, when the first activity, which is only a splashScreen with a logo was responsible to open the database in onCreate, and close it in onDestroy, and all other activities were using the database instance of that activity.

This kind of problems often do not come out when you use the application continuously, but if you send it to the background (for example simply pressing home), then run some other application, and after this when you come back to your application, you may find some of its activities killed by the system.

Check out Activity Lifecycle carefully when starting to work with Android, because it can be a bit weird at first.