活动轮换后的ProgressDialog错误行为

时间:2018-02-06 06:15:27

标签: android progressdialog

我需要显示一个ProgressDialog即使在活动轮换后也会显示。在活动progressDialog的轮换继续显示之后,即使我通过调用idle()发出信号通知它也不会被解雇。可能是什么原因。提前谢谢!

public abstract class BaseActivity extends AppCompatActivity {

    private final int command = 1;
    private final String action = "show-progress-dialog";
    private boolean showProgress = false;
    private volatile ProgressDialog progressDialog;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {

        @Override
        public void handleMessage(Message message) {
            if (message.getData() != null && message.what == command) {
                if (message.getData().getBoolean("show")) {
                    showProgressDialog();
                } else {
                    safeDismiss();
                }
            }
        }
    };

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putBoolean("showProgress", showProgress);
        safeDismiss();
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showProgress = savedInstanceState != null && savedInstanceState.getBoolean("showProgress", false);
        if (showProgress) {
            showProgressDialog();
        }
    }

    protected void busy() {
        if (Looper.getMainLooper() == Looper.myLooper()) {//in ui thread
            showProgressDialog();
            return;
        }
        Message message = mHandler.obtainMessage(command);
        Bundle data = new Bundle();
        data.putBoolean("show", true);
        data.putString("action", action);
        message.setData(data);
        message.sendToTarget();
    }

    protected void idle() {
        if (Looper.getMainLooper() == Looper.myLooper()) {//in ui thread
            safeDismiss();
            return;
        }
        Message message = mHandler.obtainMessage(command);
        Bundle data = new Bundle();
        data.putBoolean("show", false);
        data.putString("action", action);
        message.setData(data);
        message.sendToTarget();
    }

    private void showProgressDialog() {
        safeDismiss();
        progressDialog = ProgressDialog.show(this, null, "please wait…", true, false);
        showProgress = true;
    }

    private void safeDismiss() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            showProgress = false;
            progressDialog = null;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

阅读android:configChanges

  

在运行时发生配置更改时,活动将关闭   默认情况下关闭并重新启动,但使用此方法声明配置   属性将阻止活动重新启动。

<activity
    android:name=".ActivityName"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:label="@string/app_name" >
</activity>

然后

   @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

      if (progressDialog.isShowing()) 
      {
        progressDialog .dismiss();
      }
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
       if (progressDialog.isShowing()) 
      {
        progressDialog .dismiss();
      }
    }
}