android singleton dialog

◇◆丶佛笑我妖孽 提交于 2019-12-10 16:13:39

问题


I have android application which deals with lot of progress dialogs.I have to create a separate dialog for each activity.

Dialog creation takes a activity(context) as parameter while constructing.

Is there a way by which I can create a single dialog (which is tied to application and not the activity) and show it in different activity so that I do not have to create it repeatedly.


回答1:


Declare showProgressDialog and hideProgressDialog in Utill helper class as shown in following code snippet

public static ProgressDialog showProgressDialog(Context context) {
        ProgressDialog pDialog = new ProgressDialog(context);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
        return pDialog;
    }

    public static void hideProgressDialog(ProgressDialog pDialog) {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }

Then call from activity where you need to show the ProgressDialog for example in onPreExecute() method of AsyncTask class as shown in below code snippet

ProgressDialog pDialog = Util.showProgressDialog(this);

and use following code to hide the progressDialog

 Util.hideProgressDialog(pDialog);



回答2:


Putting dialog code into a helper class's static method receiving a Context maybe the best way.




回答3:


Unfortunately, no. You have to attach the dialog to an activity, otherwise your application will tend to crash. You could get exceptions like android.view.WindowManager$BadTokenException for instance.




回答4:


It's not really the answer for your question, but maybe my idea helps you. I've created a BaseActivity, there is a member dialog, activity context as member and two methods, to show and to hide progess dialog. All other activities are extended from it.




回答5:


If you want ProgressBar only in the dialog define this class

    public class ProgressDialog {
    private Dialog dialog;
    private static ProgressDialog mInstance;

    public static synchronized ProgressDialog getInstance() {
        if (mInstance == null) {
            mInstance = new ProgressDialog();
        }
        return mInstance;
    }

    public void show(Context context) {
        if (dialog != null && dialog.isShowing()) {
            return;
        }
        dialog = new Dialog(context,R.style.ProgressDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.layout_progress_dialog);
        dialog.setCancelable(false);
        dialog.show();
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}

and this XML in layout folder

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:progressDrawable="@drawable/progress"
        android:id="@+id/progress" />

</LinearLayout>

and this style in style.xml

<style name="ProgressDialog" parent="Animation.AppCompat.Dialog">
        <item name="colorAccent">@color/colorPrimary</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowTitleStyle">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:background">@android:color/transparent</item>
    </style>


来源:https://stackoverflow.com/questions/4286809/android-singleton-dialog

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