How can I open a ProgressDialog on top of a DialogFragment?

与世无争的帅哥 提交于 2019-12-09 10:22:09

问题


I have this Android activity/layout with a button. When I click this button, it opens a DialogFragment with 2 spinners. When the DialogFragment shows up, I need to populate these 2 spinners with items returned by a web service. So, while waiting for the service to return, I'd like to display a ProgressDialog over the DialogFragment I've just opened.

The problem is I can't seem to make the ProgressDialog on top of the DialogFragment. I can see the ProgressDialog being shown behind the DialogFragment, is it possible to make the ProgressDialog being displayed over everything else?

My code is like this:

public class RegionalFragment extends DialogFragment {

    private View view;
    private Activity activity;
    private Spinner spinner1;
    private Spinner spinner2;
    private ProgressDialog progressDialog;

    public RegionalFragment(Activity activity) {
        this.activity = activity;
        LayoutInflater inflater = activity.getLayoutInflater();
        view = inflater.inflate(R.layout.spinner_selection, null);
        this.progressDialog = new ProgressDialog(activity);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder =
            new AlertDialog.Builder(activity)
                    .setTitle(R.string.title)
                    .setView(view)
                    .setPositiveButton(R.string.ok, new MyListener(activity))
                    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            getDialog().cancel();
                        }
                    });

        progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(1));
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(false);
        progressDialog.setIndeterminate(true);
        progressDialog.setMessage("Please wait...");
        progressDialog.show();

        invokeAsyncWebService();

    return builder.create();
}

Thanks!


回答1:


Apparently thats a widely complained about subject with ProgressDialog, I suggest you try a normal dialog, created with the builder and whose layout (a custom one made by you) includes the progress bar or loading or whatever you need. This should solve your problem.




回答2:


I also had a problem when my parent DialogFragment was recreated due to configuration change. I couldn't make the progressDialog to be visible on top again. The solution was to show progressDialog in the onResume() event:

@Override
public void onResume() {
    super.onResume();
    if (inProgress) {
        progressDialog = ProgressDialog.show(ctx, "Report request", "connecting...", true);
    }
}


来源:https://stackoverflow.com/questions/22406223/how-can-i-open-a-progressdialog-on-top-of-a-dialogfragment

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