Background task, progress dialog, orientation change - is there any 100% working solution?

后端 未结 8 2227
半阙折子戏
半阙折子戏 2020-11-22 06:37

I download some data from internet in background thread (I use AsyncTask) and display a progress dialog while downloading. Orientation changes, Activity is rest

8条回答
  •  一整个雨季
    2020-11-22 07:27

    Did someone from Google provide some "official solution"?

    Yes.

    The solution is more of an application architecture proposal rather that just some code.

    They proposed 3 design patterns that allows an application to work in-sync with a server, regardless of the application state (it will work even if the user finishes the app, the user changes screen, the app gets terminated, every other possible state where a background data operation could be interrumpted, this covers it)

    The proposal is explained in the Android REST client applications speech during Google I/O 2010 by Virgil Dobjanschi. It is 1 hour long, but it is extremely worth watching.

    The basis of it is abstracting network operations to a Service that works independently to any Activity in the application. If you're working with databases, the use of ContentResolver and Cursor would give you an out-of-the-box Observer pattern that is convenient to update UI without any aditional logic, once you updated your local database with the fetched remote data. Any other after-operation code would be run via a callback passed to the Service (I use a ResultReceiver subclass for this).

    Anyway, my explanation is actually pretty vague, you should definititely watch the speech.

提交回复
热议问题