How to handle screen orientation change when progress dialog and background thread active?

前端 未结 28 1644
轮回少年
轮回少年 2020-11-22 07:03

My program does some network activity in a background thread. Before starting, it pops up a progress dialog. The dialog is dismissed on the handler. This all works fine, exc

28条回答
  •  执笔经年
    2020-11-22 07:46

    If you create a background Service that does all the heavy lifting (tcp requests/response, unmarshalling), the View and Activity can be destroyed and re-created without leaking window or losing data. This allows the Android recommended behavior, which is to destroy an Activity on each configuration change (eg. for each orientation change).

    It is a bit more complex, but it is the best way for invoking server request, data pre/post-processing, etc.

    You may even use your Service to queue each request to a server, so it makes it easy and efficient to handle those things.

    The dev guide has a full chapter on Services.

提交回复
热议问题