在Android Dialog中,如何为startActivityForResult设置onActivityResult?
从活动中,我可以轻松设置 onActivityResult()
并调用 startActivityForResult()
并且一切正常。
现在,我需要从对话框中调用 startActivityForResult()
。但我无法设置 onActivityResult()
,我相信 Dialog
不是 Activity
。
我如何得到结果?
我在对话框中尝试类似的操作但失败了。
//create new Intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, m_PicUri);
((Activity) getContext()).startActivityForResult(intent, Const.TAKE_PIC_ACTIVITY_RET_CODE);
From an activity, I can easily setup the onActivityResult()
and call startActivityForResult()
and everything works fine.
Now, I need to call startActivityForResult()
from the Dialog. But I can't setup the onActivityResult()
, I believe Dialog
is not an Activity
.
How do I get the result?
I try something like this inside a dialog but it failed.
//create new Intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, m_PicUri);
((Activity) getContext()).startActivityForResult(intent, Const.TAKE_PIC_ACTIVITY_RET_CODE);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以声明您的
Activity
具有Dialog
主题。看看这个问题: Android Activity 作为对话框您可以在 < code>AndroidManifest.xml 文件:
您应该能够像平常一样使用
startActivityForResult()
。我知道BluetoothChat
示例 Android 程序使用类似的方法来返回您从Dialog
列表中选择的蓝牙设备。You can declare your
Activity
to have aDialog
theme. Look into this SO question: Android Activity as a dialogYou would change this in your
AndroidManifest.xml
file:You should be able to use
startActivityForResult()
like normal. I know theBluetoothChat
example Android program uses something similar to return the Bluetooth device that you choose from aDialog
list.如果您的对话框是对话框片段,您可以
通过这种方式使用结果将发送到创建对话框的活动
if your dialog is a dialog fragment you can use
in this way the result is sent to the activity that created the dialog
您可以使用 DialogFragment 代替 Dialog。因为对话框的活动是次要的。当您使用 startActivityForResult() 启动活动时,您的对话框将被关闭
另一个使用回调的示例
创建界面
创建DialogFragment
在您的Activity<中 /strong>
输出
当您关闭DialogFragment时,您将看到“hello”登录您的Activity
You can use DialogFragment instead of Dialog. Because The dialog is secondary to its activity. When you start the activity with startActivityForResult(), your dialog gets dismissed
Another Example Use Callback
Create Interface
Create DialogFragment
In your Activity
Output
When you dismiss the DialogFragment you will see the "hello" Log in your Activity
使用兼容性包,然后使用 DialogFragment 构建对话框
Use the compatibility package then build your dialog using DialogFragment
在对话框构造函数中传递父 Activity 的引用,然后您可以在对话框中使用,如下所示:
On the dialog constructor pass the reference of parent Activity, then you can use in the dialog like this,