Muhammad Umar
Muhammad Umar

Reputation: 11782

Custom Dialog not cancelling even after setting cancellable true

I have a custom ActivityIndicator defined as this

public class ActivityIndicator extends Dialog
{
    private ImageView progress;
    private ImageView bottomProgress;

    private int type = INDICATOR_SIMPLE;

    public static final int INDICATOR_SIMPLE = 0;
    public static final int INDICATOR_BOTTOM = 1;

    public ActivityIndicator(Context context, int theme, int type)
    {
        super(context, theme);
        this.type = type;
        onCreate(null);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_indicator);

        progress = (ImageView) findViewById(R.id.progress);
        bottomProgress = (ImageView) findViewById(R.id.bottomProgress);

        if(type == INDICATOR_BOTTOM)
        {
            progress.setVisibility(View.INVISIBLE);
        }
        else if(type == INDICATOR_SIMPLE)
        {
            bottomProgress.setVisibility(View.INVISIBLE);
        }
        this.setCancelable(false);
    }

    @Override
    public void show()
    {
        progress.clearAnimation();
        bottomProgress.clearAnimation();

        if(type == INDICATOR_BOTTOM)
        {
            progress.setVisibility(View.INVISIBLE);
            new Handler().postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.rotating_img);
                    bottomProgress.startAnimation(anim);
                }
            },400);
        }

        if(type == INDICATOR_SIMPLE)
        {
            bottomProgress.setVisibility(View.INVISIBLE);
            new Handler().postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.rotating_img);
                    progress.startAnimation(anim);
                }
            },400);
        }
        super.show();
    }

    @Override
    public void dismiss()
    {
        super.dismiss();
        progress.clearAnimation();
        bottomProgress.clearAnimation();
    }
}

In my activity I initialize it as:

        indicator = new ActivityIndicator(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen, ActivityIndicator.INDICATOR_SIMPLE);

Now as seen in code , default style cancelable is false.

However at some point i do want to put it cancelable , here is my code:

        indicator.setCancelable(true);
        indicator.setOnCancelListener(new DialogInterface.OnCancelListener()
        {
            @Override
            public void onCancel(DialogInterface dialog)
            {
                finish();
            }
        });
indicator.show();

When I try to press the back button, nothing happens, the dialog doesn't cancel nor the cancel listener. What is wrong here? Why is it not cancelling automatically on back key pressed

Upvotes: 1

Views: 172

Answers (4)

the-ginger-geek
the-ginger-geek

Reputation: 7081

Don't Override onCreate(). That onCreate(null) method that you invoke is what's screwing up your code. Rather use an initializer pattern to initialize the Dialog.

If you change your onCreate to an initialize() and invoke that from the constructor the code will work.

Look at the following.

public ActivityIndicator(Context context, int theme, int type)
{
    super(context, theme);
    this.type = type;

    initialize();
}

protected void initialize()
{
    setContentView(R.layout.dialog_indicator);
    setCancelable(false);

    progress = (ImageView) findViewById(R.id.progress);
    bottomProgress = (ImageView) findViewById(R.id.bottomProgress);

    if(type == INDICATOR_BOTTOM)
    {
        progress.setVisibility(View.INVISIBLE);
    }
    else if(type == INDICATOR_SIMPLE)
    {
        bottomProgress.setVisibility(View.INVISIBLE);
    }
}

Upvotes: 2

Pankaj
Pankaj

Reputation: 8058

Got you problem just change your constructor like below and you would get your cancel listner called:

public ActivityIndicator(Context context, int theme, int type, boolean isCancelable)
    {
        super(context, theme);
        this.type = type;
        onCreate(null);
        this.setCancelable(isCancelable); //setcancelable here on the basis of boolean value and remove setcancelable from onCreate()
    }

Call the constructor with one more argument which is boolean true/false

Note: Don't forget to remove setCancelable() from onCreate() method.

Upvotes: 0

Aditi Parikh
Aditi Parikh

Reputation: 1522

When you are creating an instance of ActivityIndicator, in the OnCreate method, setCancelable is set to false.

Try removing that..

Upvotes: 0

Pavya
Pavya

Reputation: 6025

Please comment your seton cancellabel and use below code and check.

indicator.setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            finish();
        }
    }
}

Upvotes: 1

Related Questions