Android : Cancel or Interrupt runOnUiThread(Runnable) [Android]

on Friday, September 26, 2014


In my Android application the user navigates through multiple panels, and each of these panels fetches and then displays content. The content fetching happens in the background, and then after the content is fetched, I use runOnUiThread(Runnable) to display the content on the UI thread. Let's have panelSwitch be the event that is called when the user switches panels, fetchContent be the runnable called by the background thread to fetch content, and displayContent be the runnable that is dispatched by runOnUiThread() to display the content.


If the user switches panels before the content is fetched, then panelSwitch cancels/interrupts fetchContent, which in turn handles the interruption by not calling displayContent. However, I was thinking that if there is stuff already happening on the UI thread when the user switches panels, then panelSwitch is pushed to the back of the event queue. Then, if the background thread finishes fetching the content before panelSwitch reaches the front of the queue, then runOnUiThread(displayContent) will push displayContent to the end of the event queue, after panelSwitch. This means that the UI will switch panels and then the content from the previous panel will be displayed.


Is there a way to cancel/interrupt displayContent like I do with fetchContent?


0 comments:

Post a Comment