Monday, October 27, 2014

Android : Downloading expansion files after Back button pressed



I am following the example of expansion file download in https://gist.github.com/moust/7990925. The main activity registered callbacks with the downloading service:



mDownloaderClientStub = DownloaderClientMarshaller.CreateStub(this, SampleDownloaderService.class)


and assumes that Activity will be around for the callbacks defined in IDownloaderClient


I am not sure if this approach is correct since the activity that initiated expansion file download may be destroyed with the Back button. In my tests at the end of download I get call to onDownloadServiceChanged with STATE_COMPLETED even though I pressed Back button and both Activity.onStop() and Activity.onDestroy() were called.


A. I realize the downloading service has a callback reference to the activity so activity should not be garbage collected.


B. On the other hand I read about memory leaks where because activity was actually garbage collected we have phantom references to a non-existing activity. Or this is entirely different thing?


In my own solutions I use broadcasts for the service to communicate back to activity.


My questions



  1. Can I rely on activity to be around even though Activity.onDestroy() was invoked.

  2. What if Android decides to garbage collect the Activity due to memory shortage.


No comments:

Post a Comment