startActivityForResult中的requestCode是什么意思 [英] What is the meaning of requestCode in startActivityForResult

查看:84
本文介绍了startActivityForResult中的requestCode是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我是否正确地理解了requestCode的概念.该整数有什么作用,与我将其设置为哪个整数无关紧要:

I'm wondering if I am understanding the concepts of requestCode correectly. What is this integer for and does it matter what integer I set it to in:

private static int CAMERA_REQUEST = ???;

谢谢

推荐答案

requestCode可帮助您确定从哪个Intent返回.例如,假设您的活动A(主要活动)可以调用活动B(摄像机请求),活动C(录音),活动D(选择联系人).

The requestCode helps you to identify from which Intent you came back. For example, imagine your Activity A (Main Activity) could call Activity B (Camera Request), Activity C (Audio Recording), Activity D (Select a Contact).

每当随后调用的活动B,C或D完成并且需要将数据传递回A时,现在您都需要在onActivityResult中标识您要从中返回哪个Activity并相应地放置处理逻辑.

Whenever the subsequently called activities B, C or D finish and need to pass data back to A, now you need to identify in your onActivityResult from which Activity you are returning from and put your handling logic accordingly.




    public static final int CAMERA_REQUEST = 1;
    public static final int CONTACT_VIEW = 2;

    @Override
    public void onCreate(Bundle savedState)
    {
        super.onCreate(savedState);
        // For CameraRequest you would most likely do
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);

        // For ContactReqeuest you would most likely do
        Intent contactIntent = new Intent(ACTION_VIEW, Uri.parse("content://contacts/people/1"));
        startActivityForResult(contactIntent, CONTACT_VIEW);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (resultCode == Activity.RESULT_CANCELED) {
            // code to handle cancelled state
        }
        else if (requestCode == CAMERA_REQUEST) {
            // code to handle data from CAMERA_REQUEST
        }
        else if (requestCode == CONTACT_VIEW) {
            // code to handle data from CONTACT_VIEW
        }
    }


我希望这可以澄清参数的使用.

I hope this clarifies the use of the parameter.

这篇关于startActivityForResult中的requestCode是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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