使用Android版的onActivityResult [英] Use onactivityresult android

查看:190
本文介绍了使用Android版的onActivityResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打电话从 mainactivity 的方法在其他活动。对于这一点,我已经研究了很多,发现使用的onActivityResult 是最好的选择。任何人都可以请解释如何使用此方法用一个例子的帮助?我已经通过类似的问题走了,但发现他们困惑。
谢谢!

I want to call a method from mainactivity in other activities. For that, I've researched a lot and found that using OnActivityResult is the best option. Can anyone please explain how to use this method with the help of an example? I've gone through similar questions but found them confusing. Thanks!

编辑:我在我的应用程序自定义对话框的活动。它会询问是否要开始一个新游戏或者没有用户,它有两个按钮yes和no。我只想实现上述方法得到的pressed按钮。

I have a custom dialog activity in my app. It asks the users whether they want to start a new game or not and it has two buttons yes and no. I want to implement the above method only to get the pressed button.

推荐答案

定义常量

public static final REQUEST_CODE = 1;

使用意向请致电您的自定义对话框活动

Call your custom dialog activity using intent

Intent intent = new Intent(Activity.this,
                    CustomDialogActivity.class);
            startActivityForResult(i, REQUEST_CODE);

现在使用的onActivityResult检索结果

Now use onActivityResult to retrieve the result

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == RESULT_OK) {

                String requiredValue = data.getStringExtra("Key");
            }
        } catch (Exception ex) {
            Toast.makeText(Activity.this, ex.toString(),
                    Toast.LENGTH_SHORT).show();
        }

    }

在自定义对话框活动使用code设置结果

In custom dialog activity use this code to set result

Intent intent = getIntent();
intent.putExtra("key", value);
setResult(RESULT_OK, intent);
finish();

这篇关于使用Android版的onActivityResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆