UI updates across config changes: The weird point

只愿长相守 提交于 2019-12-11 17:55:09

问题


I am an experienced Android developer and it is a shame that I am still ineffectively fighting against fundamental issues like persisting UI changes across configuration changes.

I know, this is an all too well known problem and there exists a lot of literature on it. The recommended solution is to setRetainInstance(true) a UI-less fragment:

https://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html

Best practice: AsyncTask during orientation change

For the sake of completeness, let me draw up the scenario. You have an AsyncTask that performs a Network request and is supposed to update the UI in onPostExecute()

I see some issues with the proposed solution:

  • The proposed solution says that onPostExecute() is guaranteed to not be called during between the calls to onDetach() and the next onAttach() but what if the AsyncTask returns early(after Activity's onPause() and before the call to onDetach()). If onPostExecute() executes during the period, since onSaveInstanceState() will have been called, any update to the Views will not persist across Activity restart.
  • Second if the AsyncTask returns such that onPostExecute() executes before the call to Activity's onCreate() then the views will not have been initialized for any view updates to be pushed yet.

Is there any effective solution against this problem that addresses memory leaks and state save across config changes?

来源:https://stackoverflow.com/questions/53603796/ui-updates-across-config-changes-the-weird-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!