API Demos 2.2 研读笔记(9)——通过调用子Activity返回值
我们使用Intent可以将数据从一个Activity传递到下一个Activity,同样,在Android中我们可以将数据从一个Activity返回给前一个Activity。
?
参考API Demo示例:
?
1. 定义父Activity,ReceiveResult.java。在这个Activity中我们通过startActivityForResult(intent, GET_CODE)启动子Activity。
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 { @Overrideprotected 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); } @Overrideprotected 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。
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{ @Overrideprotected 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的处理结果,对不同的结果码进行相应的操作。