在Android Dialog中,如何为startActivityForResult设置onActivityResult?

发布于 2024-12-01 19:22:37 字数 545 浏览 2 评论 0原文

从活动中,我可以轻松设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凹づ凸ル 2024-12-08 19:22:37

您可以声明您的 Activity 具有 Dialog 主题。看看这个问题: Android Activity 作为对话框

您可以在 < code>AndroidManifest.xml 文件:

<activity android:theme="@android:style/Theme.Dialog" />

您应该能够像平常一样使用 startActivityForResult() 。我知道 BluetoothChat 示例 Android 程序使用类似的方法来返回您从 Dialog 列表中选择的蓝牙设备。

You can declare your Activity to have a Dialog theme. Look into this SO question: Android Activity as a dialog

You would change this in your AndroidManifest.xml file:

<activity android:theme="@android:style/Theme.Dialog" />

You should be able to use startActivityForResult() like normal. I know the BluetoothChat example Android program uses something similar to return the Bluetooth device that you choose from a Dialog list.

别挽留 2024-12-08 19:22:37

如果您的对话框是对话框片段,您可以

getActivity().startActivityForResult(intent);

通过这种方式使用结果将发送到创建对话框的活动

if your dialog is a dialog fragment you can use

getActivity().startActivityForResult(intent);

in this way the result is sent to the activity that created the dialog

宁愿没拥抱 2024-12-08 19:22:37

您可以使用 DialogFragment 代替 Dialog。因为对话框的活动是次要的。当您使用 startActivityForResult() 启动活动时,您的对话框将被关闭

另一个使用回调的示例

创建界面

 public interface DialogCallback {
   void getResults(String results);
 }

创建DialogFragment

public class DialogFragment extends DialogFragment {

DialogCallback dialogCallback;

public DialogFragment setCallBack(DialogCallback dialogCallback){
    this.dialogCallback = dialogCallback;
    return this;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return super.onCreateDialog(savedInstanceState);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.your_layout, container, false);
    return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    dialogCallback.getResults("hello");
}

}

在您的Activity<中 /strong>

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     new DialogFragment().setCallBack(dialogCallback).show(getFragmentManager(),"");
}

DialogCallback dialogCallback = new DialogCallback() {
    @Override
    public void getResults(String results) {
        if(results!=null){
            Log.e(TAG,results);
        }
    }
};

输出

当您关闭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

 public interface DialogCallback {
   void getResults(String results);
 }

Create DialogFragment

public class DialogFragment extends DialogFragment {

DialogCallback dialogCallback;

public DialogFragment setCallBack(DialogCallback dialogCallback){
    this.dialogCallback = dialogCallback;
    return this;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return super.onCreateDialog(savedInstanceState);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.your_layout, container, false);
    return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    dialogCallback.getResults("hello");
}

}

In your Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     new DialogFragment().setCallBack(dialogCallback).show(getFragmentManager(),"");
}

DialogCallback dialogCallback = new DialogCallback() {
    @Override
    public void getResults(String results) {
        if(results!=null){
            Log.e(TAG,results);
        }
    }
};

Output

When you dismiss the DialogFragment you will see the "hello" Log in your Activity

℡Ms空城旧梦 2024-12-08 19:22:37

使用兼容性包,然后使用 DialogFragment 构建对话框

Use the compatibility package then build your dialog using DialogFragment

丿*梦醉红颜 2024-12-08 19:22:37

在对话框构造函数中传递父 Activity 的引用,然后您可以在对话框中使用,如下所示:

parentActivity.startActivityForResult(intent, CODE);

On the dialog constructor pass the reference of parent Activity, then you can use in the dialog like this,

parentActivity.startActivityForResult(intent, CODE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文