从对话框或活动返回结果
我想知道是否可以冻结当前活动,同时等待另一个活动或对话框(任何都可以)完成。
我知道我可以启动一个结果活动,并在那里处理这些活动,但是 startActivityForResult() 之后的代码仍然会被执行,
这是我想做的事情:
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您还可以使用
onActivityResult
在您的主要活动中调用
startActivityForResult(意图, 1); //这里 1 是请求代码
在你的 Dialog 类中,
所以你的主要活动
You can use
onActivityResult
alsoIn your main activity call
startActivityForResult(intent, 1); //here 1 is the request code
In your Dialog class
so your main activity
在对话框中,如果您期望得到结果,则应该使用回调方法,这些方法可以在您单击对话框的按钮时执行。
例如:
这样,当您运行代码时,
onClick
方法将不会执行,但当点击对话框内的任何按钮时,它将执行。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:
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
尝试为对话框提供一个按钮,通过对活动中某些内容的方法调用来实现
onClickListener
。所述方法中的代码仅在单击按钮时运行,因此您需要使用按钮的不同参数来调用该方法,具体取决于按钮的操作。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.您可以使用
onActivityResult
。这是我的代码。
当您开始活动时。
例如)您从 MainActivity 调用 TestActivity 您可以这样做。
在测试活动中。
当你在MainActivity上获取结果代码时,你可以这样做。
<前><代码>@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.REQUEST_CODE && resultCode == Activity.RESULT_OK) {
// 做某事...
} else if (requestCode == Constants.REQUEST_CODE && resultCode == Activity.RESULT_CANCELED) {
// 做某事...
}
}
You can use
onActivityResult
.here is my code.
when you start activity.
ex) you call TestActivity from MainActivity you can do like this.
in TestActivity.
when you get result code on MainActivity, you can do like this.
对于那些对实现对话框以获取结果的方式感兴趣,但不使用onActivityResult的人来说,这里是一个使用回调的示例。这样您就可以从任何地方调用此自定义对话框并根据选择执行某些操作。
快捷方式
另一种方式,如果您必须实现不同的对话框变体,则适合,因为您可以在一个位置定义所有操作。
MyDialog.java
像这样使用它
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
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
Use it like this
我也不太明白。如果您想从活动中捕获结果,那么您只需按照您提到的“startActivityForResult”函数开始即可。如果您想在对话框中捕获一些结果,那么您可以简单地将所有函数(在按下对话框上的按钮后应该继续)放入对话框上每个按钮的 onClickListener 中
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
我迟了好几年才回答这个问题,但这仍然是我的答案。
我有一个代表表单/文件的类。它有一个公共成员“deleteDialog()”,允许按需删除文件,并向调用者返回“true”或“false”值。
它看起来像这样:
您将看到结果变量 - “mDeleted” - 必须是封闭类的成员;这是由于 Java 的奇怪但奇妙的变幻莫测。其中内部类(在本例中:“DialogInterface.OnClickListenerdialogClickListener”)可以继承其外部类的状态。
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:
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.