简体   繁体   中英

What is the best way of creating a reusable dialog with Activity's lifecycle?

I'd like to build..

  • A reusable dialog
  • Fully customizable layout (custom color or typeface, etc)
  • Maintain its lifecycle with referenced Activity
  • Use overloading which I can create several variations of dialog, ie combination of title, message and callbacks
  • In a simple way (if possible)

Currently my custom dialog class looks like :

public class CustomAlert {

public interface OnSingleClickedListener {
    public void onPositiveClicked();
}

public interface OnDualClickedListener {
    public void onPositiveClicked();
    public void onNegativeClicked();
}

/**
 * Show simple alert without callback.
 * @param context
 * @param msg
 */
public static void showAlert(Context context, String msg) {
    final Dialog dialog = new Dialog(context);
    // Do some stuff

    ok.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View view) {
            dialog.dismiss();
        }
    });
    dialog.show();
}

/**
 * Show simple alert with callback.
 * @param context
 * @param msg
 * @param listener
 */
public static void showAlert(Context context, String msg, final OnSingleClickedListener listener) {
    final Dialog dialog = new Dialog(context);
    // Do some stuff

    ok.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View view) {
            dialog.dismiss();
            listener.onPositiveClicked();
        }
    });
    dialog.show();
}
// Some other methods..
}

and I call these alerts from Activity like this :

if(!isFinishing()) {
    CustomAlert.showAlert(MainActivity.this, getResources().getString(R.string.network_no_connection));
}

Even though I'm calling isFinishing() to check if the host Activity is running, I keep seeing BadTokenException , is your Activity running? , and I thought maybe isFinishing() is not enough.

I found this article which is using DialogFragment , but I feel like this is quite a lot of code for such a small task when I consider above requirements.

What is the most recommended and effective solution to solve this problem?

Thanks in advance!

Create a custom class like this,i have created this dialog for thumbs up and thumbs down in my application named Debongo - Restaurant Finder on play store

//recommend dialog
public void showRecommendDialog(Context mContext)
{
     dialog = new Dialog(mContext,android.R.style.Theme_Holo_Dialog_NoActionBar);
     dialog.setContentView(R.layout.recommend_dialog);
     dialog.show();

    btnThumbsUp = (ImageButton) dialog.findViewById(R.id.btn_Yes);
    btnThumbsDown = (ImageButton) dialog.findViewById(R.id.btn_no);
    txtMsg=(TextView) dialog.findViewById(R.id.txtMsg);

    ImageButton cancelRecommend=(ImageButton) dialog.findViewById(R.id.cancelRecommend);
    cancelRecommend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            dialog.dismiss();
            dialog=null;
        }
    });

    btnThumbsUp.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //do what you want
        }
    });


    btnThumbsDown.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //do what you want
        }
    });
}

Here is the layout file for the same

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:gravity="center"
android:padding="5dp" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp" >

    <ImageButton
        android:id="@+id/cancelRecommend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@android:color/transparent"
        android:contentDescription="@null"
        android:src="@drawable/com_facebook_close" />

    <TextView
        android:id="@+id/txtMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/cancelRecommend"
        android:layout_marginBottom="@dimen/viewSpace3"
        android:layout_marginLeft="@dimen/viewSpace3"
        android:layout_marginRight="@dimen/viewSpace3"
        android:text="Do You Want To Recommend This Restaurant ?"
        android:textColor="#000000"
        android:textSize="@dimen/titlebar_textSize"
        tools:ignore="HardcodedText" />

    <View
        android:layout_width="0dip"
        android:layout_height="@dimen/viewSpace3" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtMsg"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center" >

        <ImageButton
            android:id="@+id/btn_Yes"
            android:layout_width="@dimen/viewSpace5"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:contentDescription="@null"
            android:src="@drawable/unselected_thumbs_up" />

        <View
            android:id="@+id/vvv"
            android:layout_width="@dimen/viewSpace1"
            android:layout_height="@dimen/viewSpace3"
            android:layout_toRightOf="@id/btn_Yes" />

        <ImageButton
            android:id="@+id/btn_no"
            android:layout_width="@dimen/viewSpace5"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/vvv"
            android:background="@android:color/transparent"
            android:contentDescription="@null"
            android:src="@drawable/unselected_thumbs_down" />

    </RelativeLayout>

</RelativeLayout>

Now when you want to open this dialog,you can just call it by using

showRecommendDialog(this);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM