Here is a situation,
I have an android activity which starts a service (The service is meant to send/receive files etc). The service sendMessage() to the Activity via the handler created in Activity class object.
The Activity then shows the status of current action(s) service is performing on the views it contains.
Here is the problem
- Activity starts
- Activity created Handler in a global object
- Activity starts the service –
- User through the activity commands the service to begin the process of send/receive files
- The service is now busy and also sending messages to UI through the handler obtained via the global object
- The Activity receives and handleMessage() and publish the state on UI
- The user now sends my Activity to background (may be he starts using another app)
- During this process android probably kills my Activity but service is busy/running
- Service in the mean time finishes
- After some time the user opens the Activity,
- (The problem) The Activity starts onCreate is called the activity does not know the last status of the service. whether it finished successfully or with failure or any other status last status..
SharedPreferences, SQLite, text files or simply static variable or variable/field of Application instance (implement singleton of Application to getInstance in any place of your code). Static variable and application instance will lose states in memory once killed by android.