Question

I am showing a custom dialog in my application, which stays for a small time, (say 2 seconds) and then disappears. I am calling this custom dialog from the main activity. I want my calling activity to be paused till the custom dialog disappears. The issue is that activity code after the dialog is dismissed is always executed, while the dialog is being shown.

I have tried showing the dialog, sleeping for 2 seconds and then dismiss it, which is not working. The activity code after the dialog is dismissed is executing.

cust_dlg.show();
Thread.sleep(2000);
cust_dlg.dismiss();

I have also tried putting the dialog in a timer task, which also fails

        final Timer t = new Timer();
        cust_dlg.show();
        t.schedule(new TimerTask() {
        public void run() {
        cust_dlg.dismiss();
        t.cancel();
        }
        }, 500);

I have also tried simple threading with the custom dialog in a thread and put sleep simultaneously in the main activity, which causes exception and force closes.

Somebody please tell me a way to pause the main activity while the custom dialog is shown, so that the code after the custom dialog dismiss is NOT executed.

Thanks

Was it helpful?

Solution

Try a Handler

        Handler handler = new Handler(); 
        handler.postDelayed(new Runnable() { 
             public void run() { 


        AlertDialog.Builder builder = new AlertDialog.Builder(RBLQuizSpiel.this);
        if (aktuellGeloest == true){

        builder.setMessage(R.string.quiz_neu)
                .setTitle(R.string.quiz_glueckwunsch)
                .setPositiveButton(R.string.quiz_ja, dialogClickListener)
                .setNegativeButton(R.string.quiz_nein, dialogClickListener).show();
        } else {
            builder.setMessage(R.string.quiz_neu)
            .setTitle(R.string.quiz_schade)
            .setPositiveButton(R.string.quiz_ja, dialogClickListener)
            .setNegativeButton(R.string.quiz_nein, dialogClickListener).show();

        }
        }
}, 1500);

no more code behind this.

1500 = ms

Well an you need to set the execution of the following code in the Clicklistner. Android is event-driven OS. The Dialog will be put in a message qeue and your program steps on..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top