当前位置:操作系统 > 安卓/Android >>

Android ApiDemos示例解析(11):App->Activity->Receive Result

startActivity可以启动一个新的Activity,这个新的Activity与启动它Activity之间相对独立,不会返回给调用它的Activity如何结果。在写应用时,有时需要从其它Activity返回结果。Receive Result显示了这个用法。
startActivityForResult 使用方法和startActivity类似,重要的是startActivityForResult可以从被调用的Activity返回结果。GET_CODE为Request code,当子Activity返回结果时可以根据这个Request Code来处理Activity返回的结果。

[java] 
// Definition of the one requestCode we use for receiving resuls.  
static final private int GET_CODE = 0; 
  
private OnClickListener mGetListener = new OnClickListener() { 
    public void onClick(View v) { 
        // Start the activity whose result we want to retrieve.  The  
        // result will come back with request code GET_CODE.  
        Intent intent = new Intent(ReceiveResult.this, SendResult.class); 
        startActivityForResult(intent, GET_CODE); 
    } 
}; 
// Definition of the one requestCode we use for receiving resuls.
static final private int GET_CODE = 0;
 
private OnClickListener mGetListener = new OnClickListener() {
    public void onClick(View v) {
        // Start the activity whose result we want to retrieve.  The
        // result will come back with request code GET_CODE.
        Intent intent = new Intent(ReceiveResult.this, SendResult.class);
        startActivityForResult(intent, GET_CODE);
    }
};
当子Activity结束时,调用setResult 来设置返回结果。Receive Result例子中涉及了两个Activity, ReceiveResult为父Activity,SendResult为子Activity返回用户选项,返回的结果在ReceiveResult中显示。
SendResult 返回结果的代码如下:

[java] 
private OnClickListener mCorkyListener = new OnClickListener() 

    public void onClick(View v) 
    { 
        // To send a result, simply call setResult() before your  
        // activity is finished.  
        setResult(RESULT_OK, (new Intent()).setAction("Corky!")); 
        finish(); 
    } 
}; 
  
private OnClickListener mVioletListener = new OnClickListener() 

    public void onClick(View v) 
    { 
        // To send a result, simply call setResult() before your  
        // activity is finished.  
        setResult(RESULT_OK, (new Intent()).setAction("Violet!")); 
        finish(); 
    } 
}; 
private OnClickListener mCorkyListener = new OnClickListener()
{
    public void onClick(View v)
    {
        // To send a result, simply call setResult() before your
        // activity is finished.
        setResult(RESULT_OK, (new Intent()).setAction("Corky!"));
        finish();
    }
};
 
private OnClickListener mVioletListener = new OnClickListener()
{
    public void onClick(View v)
    {
        // To send a result, simply call setResult() before your
        // activity is finished.
        setResult(RESULT_OK, (new Intent()).setAction("Violet!"));
        finish();
    }
};
当从子Activity返回结果后,父Activity通过定义onActivityResult来处理子Activity返回的结果。

[java] 
protected void onActivityResult(int requestCode, int resultCode, 
        Intent data) { 
        // You can use the requestCode to select between multiple child  
        // activities you may have started.  Here there is only one thing  
        // we launch.  
        if (requestCode == GET_CODE) { 
  
            // We will be adding to our text.  
           ... 
        } 
    } 
protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {
        // You can use the requestCode to select between multiple child
        // activities you may have started.  Here there is only one thing
        // we launch.
        if (requestCode == GET_CODE) {
 
            // We will be adding to our text.
           ...
        }
    }
onActivityResult中的三个参数:
requestCode: 就是在调用startActivityForResult 时的request code,可以使用request code 来区分多个子Acitivity。
resultCode:为子Activity的返回值,一般为RESULT_OK (正常),RESULT_CANCELLED(取消)。
Data: Activity的返回结果。例子中为用户选择的Corky或是Violet。

 

 \

 作者:mapdigit
 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,