21

I would like to know if I can freeze the current Activity, while I wait for another activity or dialog (any would do) to finish.

I know I can start an activity for results, and handle those there, but the code after startActivityForResult() will still get executed

this is something I would like to do:

PopupDialog dialog = new PopupDialog(this,android.R.style.Theme_Black_NoTitleBar);
dialog.show();
// wait here, and continue the code after the dialog has finishes
int result = getResultFromDialogSomehow();
if (result == 1){
    //do something
}else{
    //do something else
}

I know this must sound quite weird, but but I would really appreciate it if anyone can tell me how to achieve such functionality.

zidarsk8
  • 3,088
  • 4
  • 23
  • 30
  • 2
    I can't get you.You want to get result of Dialog or Activity ? – Dharmendra Jul 29 '11 at 03:39
  • There was a typo there, sorry, and any would do. I would just like a way to show something on the screen, and then continue the code from there. In a normal workflow, all the code you see there would finish before a dialog would show on screen. – zidarsk8 Jul 29 '11 at 04:02

8 Answers8

14

You can use onActivityResult also
In your main activity call
startActivityForResult(intent, 1); //here 1 is the request code

In your Dialog class

Intent intent = new Intent();
intent.putExtra(....) //add data if you need to pass something
setResult(2,intent); //Here 2 result code

so your main activity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == 2 && requestCode ==1){
    //do something
}else{
    //do something else
}
}
Labeeb Panampullan
  • 34,521
  • 28
  • 94
  • 112
  • 1
    Thank you, and this is the normal way of using this. But this sort of breaks up the code. And my question is, could I call an activity, and get the result in the same function, and continue with the work. See my comment to the previous answer. – zidarsk8 Jul 29 '11 at 18:01
  • 3
    Where do you see a `setResult` method in a dialog? – Swato Aug 15 '12 at 07:30
  • 1
    you can cast you `getContext()` to `Activity`, `((Activity) getContext()).setResult(2,intent);;` – Labeeb Panampullan Aug 16 '12 at 05:01
  • Related post: http://stackoverflow.com/questions/10905312/receive-result-from-dialogfragment – Roger Huang Nov 25 '15 at 16:20
10

In a dialog, if you expect a result, you should use callback methods which can are executed when you click on the dialog's buttons.

For example:

AlertDialog.Builder builder = new AlertDialog.Builder(getDialogContext());
builder.setMessage("Message");
builder.setPositiveButton("Yes", new Dialog.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) { 
        Toast.makeText(this, "Yes", Toast.LENGTH_SHORT).show();
        dialog.cancel();
    }

});

builder.setNegativeButton("No", new Dialog.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(this, "No", Toast.LENGTH_SHORT).show();
        dialog.cancel();

    }

});

builder.show();

This way, onClick method will not execute when you run the code, but it will execute when any of your buttons inside the dialog are tapped.

dorkdork
  • 15
  • 1
  • 4
Dharmendra
  • 33,296
  • 22
  • 86
  • 129
3

I use a callback in my Dialog to return some String or Value that the user selected.

i.e. implement an interface in your Dialog

Someone Somewhere
  • 23,475
  • 11
  • 118
  • 166
1

You can use onActivityResult.

here is my code.

  1. when you start activity. ex) you call TestActivity from MainActivity you can do like this.

    Constants.REQUEST_CODE = 1000; // this is a global variable...and it must be a unique.
    ....
    Intent intent = new Intent(this, TestActivity.class);
    startActivityForResult(intent, Constants.REQUEST_CODE);
    
  2. in TestActivity.

    public void onClickCancel() {
        setResult(Activity.RESULT_CANCELED);
        finish();
    }
    
    public void onClickConfirm() {
        setResult(Activity.RESULT_OK);
        finish();
    }
    
  3. when you get result code on MainActivity, you can do like this.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (requestCode == Constants.REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            // todo something...
        } else if (requestCode == Constants.REQUEST_CODE && resultCode == Activity.RESULT_CANCELED) {
            // todo something...
        }
    }
    
Rooney
  • 1,195
  • 10
  • 18
1

For those who are interrested in a way to implement a dialog box to get a result, but without using onActivityResult here is an example using callbacks. This way you can call this custom dialog box from anywhere and do something according to the choice.

A SHORT WAY

public void getDialog(Context context, String title, String body, 

    DialogInterface.OnClickListener listener){

        AlertDialog.Builder ab = new AlertDialog.Builder(context);
        ab
                .setTitle(title)
                .setMessage(body)
                .setPositiveButton("Yes", listener)
                .setNegativeButton("Cancel", listener)
        ;//.show();

        Dialog d=ab.create();
        d.setCanceledOnTouchOutside(false);

        d.show();
    }

    private void showDialog(){
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                    case DialogInterface.BUTTON_POSITIVE:
                        //DO
                        break;

                    case DialogInterface.BUTTON_NEGATIVE:
                        //DO
                        break;
                }
            }
        };

        getDialog(
                this,
                "Delete",
                "Are you sure to delete the file?",
                dialogClickListener
        );

    }

Another way, suitable if you have to implement different variations of dialog boxes since you can define the all the actions in a single place.

MyDialog.java

public class MyDialog{

    public void deleteDialog(Context context){
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                    case DialogInterface.BUTTON_POSITIVE:
                        if(listener!=null)
                            listener.onDeleteDialogResponse(true);
                        break;

                    case DialogInterface.BUTTON_NEGATIVE:
                        if(listener!=null)
                            listener.onDeleteDialogResponse(false);
                        break;
                }
            }
        };

        AlertDialog.Builder ab = new AlertDialog.Builder(context);
        ab.setMessage("Are you sure to delete?")
                .setPositiveButton("Yes", dialogClickListener)
                .setNegativeButton("Cancel", dialogClickListener)
                .show();


    }

/** my listner */
    public interface MyDialogListener{
        public void onDeleteDialogResponse(boolean respononse);
    }
    private MyDialogListener listener;

    public void setListener(MyDialogListener listener) {
        this.listener = listener;
    }

}

Use it like this

private void showDialog(){        
        MyDialog dialog=new MyDialog();
        dialog.setListener(new MyDialog.MyDialogListener() {
            @Override
            public void onDeleteDialogResponse(boolean respononse) {
                if(respononse){
                    //toastMe("yessss");
                    //DO SOMETHING IF YES
                }else{
                    //toastMe("noooh");
                    //DO SOMETHING IF NO
                }
            }
        });

            dialog.deleteDialog(this);
}
chandima
  • 121
  • 4
1

Try giving the dialog a button, implementing an onClickListener with a method call to something in your activity. The code in said method will only be run when the button is clicked, so you'd want to call that method with a different parameter for the buttons, depending on what they did.

thegrinner
  • 11,546
  • 5
  • 41
  • 64
0

I am several years late in answering this question, but here is my answer, nonetheless.

I have a class that represents a form/file. It has a public member "deleteDialog()" that allows for delering the file on demand, and it returns a "true" or "false" value to the caller.

Here is what it looks like:

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

public class Form {

private Context mContext;
private CharSequence mFilePath;
private boolean mDeleted = false; // Set to "true" if this file is deleted. 
    /*...etc etc...*/

public boolean deleteDialog() {
    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
        //@Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
            case DialogInterface.BUTTON_POSITIVE:
                //Do your Yes progress
                mDeleted = mContext.deleteFile(mFilePath.toString());
                break;

            case DialogInterface.BUTTON_NEGATIVE:
                //Do your No progress
                mDeleted = false;
                break;
            }
        }
    };
    AlertDialog.Builder ab = new AlertDialog.Builder(mContext);
    ab.setMessage("Are you sure to delete?")
        .setPositiveButton("Yes", dialogClickListener)
        .setNegativeButton("Cancel", dialogClickListener)
        .show();
    return mDeleted;
}

You will see that the result variable - "mDeleted" - must be a member of the enclosing class; this is due to the strange but wonderful vagaries of Java. where an inner-class (in this case: "DialogInterface.OnClickListener dialogClickListener") can inherit the state of it's outer class.

0

I also do not fully understand. If you want to catch result from activity, then you can simply start as you menitoned "startActivityForResult" function. If you want to catch some results in dialog, then you can simply place all functions (that should continue after you press button on dialog) into onClickListener of every button on dialog

Menion Asamm
  • 984
  • 11
  • 19
  • the simpelest way of putting it would be that I would like to get something like this `int resultCode = showCustomDialog()`. I know I can get results from a dialog, but I don't if it is possible to return them like this, cause it would make a lot of code look much nicer. – zidarsk8 Jul 29 '11 at 17:58
  • hmm I probably understand. You want to same behavior as is classic J2SE right? I'm worried this is not possible in android. I really suggest way that wrote Labeeb P or with some custom dialog that will extend android.app.Dialog and some interface that will handle all return possibility – Menion Asamm Jul 30 '11 at 07:16
  • 1
    @zidarsk8 i also facing same problem now do you found any solution for this – Mr.Cool Mar 08 '13 at 12:22