Android 2.2 API Demos -- 通过调用子Activity返回值

我们使用Intent可以将数据从一个Activity传递到下一个Activity,同样,在Android中我们可以将数据从一个Activity返回给前一个Activity。

参考API Demo示例:

1. 定义父Activity,ReceiveResult.java。在这个Activity中我们通过startActivityForResult(intent, GET_CODE)启动子Activity。
Java代码

package com.example.android.apis.app;

import java.util.Map;

import com.example.android.apis.R;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import java.util.Map;

public class ReceiveResult extends Activity {
       @Override
        protected void onCreate(Bundle savedInstanceState) {
        // Be sure to call the super class.
        super.onCreate(savedInstanceState);

        // See assets/res/any/layout/hello_world.xml for this
        // view layout definition, which is being set here as
        // the content of our screen.
        setContentView(R.layout.receive_result);

        // Retrieve the TextView widget that will display results.
        mResults = (TextView)findViewById(R.id.results);

        // This allows us to later extend the text buffer.
        mResults.setText(mResults.getText(), TextView.BufferType.EDITABLE);

        // Watch for button clicks.
        Button getButton = (Button)findViewById(R.id.get);
        getButton.setOnClickListener(mGetListener);
    }

  @Override
        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.
            Editable text = (Editable)mResults.getText();

            // This is a standard resultCode that is sent back if the
            // activity doesn't supply an explicit result.  It will also
            // be returned if the activity failed to launch.
            if (resultCode == RESULT_CANCELED) {
                text.append("(cancelled)");

            // Our protocol with the sending activity is that it will send
            // text in 'data' as its result.
            } else {
                text.append("(okay ");
                text.append(Integer.toString(resultCode));
                text.append(") ");
                if (data != null) {
                    text.append(data.getAction());
                }
            }

            text.append("\n");
        }
    }

    // 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);
        }
    };

    private TextView mResults;
}


我们需要为startActivityForResult传递一个Intent和一个请求码。

Intent决定启动哪个Activity。请求码是对子Activity标记的唯一ID。假如从一个父Activity中可能启动的子Activity有多个,那么通过请求码我们就可以知道是从哪个子Activity返回的。

 

2. 定义子Activity,SendResult.java。在这个Activity中我们通过setResult(RESULT_OK, (new Intent()).setAction("Corky!"))将结果返回给父Activity。
Java代码

package com.example.android.apis.app;

import com.example.android.apis.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SendResult extends Activity
{
    @Override
        protected void onCreate(Bundle savedInstanceState)
    {
        // Be sure to call the super class.
        super.onCreate(savedInstanceState);

        // See assets/res/any/layout/hello_world.xml for this
        // view layout definition, which is being set here as
        // the content of our screen.
        setContentView(R.layout.send_result);

        // Watch for button clicks.
        Button button = (Button)findViewById(R.id.corky);
        button.setOnClickListener(mCorkyListener);
        button = (Button)findViewById(R.id.violet);
        button.setOnClickListener(mVioletListener);
    }

    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();
        }
    };
}


在调用finish()之前,要先调用setResult方法,将结果码和数据返回给父Activity。在android.app.Activity中定义两个标准结果码,RESULT_OK和RESULT_CANCELED。

如果子Activity启动失败或者没有显式的在setResult()中设置结果码,子Activity会默认返回RESULT_CANCELED。

3. 在父Activity中,我们重写onActivityResult(int requestCode, int resultCode,  Intent data)方法,接收子Activity返回的数据。参考1中的代码。

请求码(requestCode):通过它我们可以对多个子Activity进行处理。

结果码(resultCode):通过它我们可以判断子Activity的处理结果,对不同的结果码进行相应的操作。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值