Returning from a dialog or activity with result
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.
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
}
}
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.
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
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.
You can use onActivityResult
.
here is my code.
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);
in TestActivity.
public void onClickCancel() { setResult(Activity.RESULT_CANCELED); finish(); } public void onClickConfirm() { setResult(Activity.RESULT_OK); finish(); }
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... } }
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);
}
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
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.
精彩评论