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

前端 未结 28 1647
轮回少年
轮回少年 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:47

    If you maintain two layouts, all UI thread should be terminated.

    If you use AsynTask, then you can easily call .cancel() method inside onDestroy() method of current activity.

    @Override
    protected void onDestroy (){
        removeDialog(DIALOG_LOGIN_ID); // remove loading dialog
        if (loginTask != null){
            if (loginTask.getStatus() != AsyncTask.Status.FINISHED)
                loginTask.cancel(true); //cancel AsyncTask
        }
        super.onDestroy();
    }
    

    For AsyncTask, read more in "Cancelling a task" section at here.

    Update: Added condition to check status, as it can be only cancelled if it is in running state. Also note that the AsyncTask can only be executed one time.

提交回复
热议问题