简体   繁体   中英

Call to main activity in onPostExecute (after activity is destroyed or in stopped state)

In my application, I'm having an AsyncTask that uploads some data to the server and interacts with the calling activity using an interface "FetchCompleteListener " that I wrote.

The calling activity implements a FetchCompleteListener and passes its own instace to the AsyncTask.

The given AsyncTask calls FetchCOmpleetListener's onFetchCoMplete in onPostExecute()

My question is - what happens in the following scenarios:

  1. When the given activity is in stopped state
  2. When the given activity is finished/destroyed?

Is this a case of memory leak? Will my application crash when I try to call onFetchComplete in onPostExecute()?

You should always decouple background tasks from your ui. you can do one of the following:

  1. Your ui will register a broadcast receiver inside onResume (of course unregister that in onPause), the background task will send a broadcast that will be caught by the ui when it is active

  2. You can queue up your result in some service classes (or what suits you best), when the corresponding ui comes alive it will take results from queue (based on some TAG)

  3. you can use an EventBus like Otto or greenrobot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM