Android进阶2之Activity之间数据交流(onActivityResult的用法) (转载)
转自:http://blog.csdn.net/sjf0115/article/details/7387467
主要功能:
在一个主界面(主Activity)上能连接往许多不同子功能模块(子Activity上去),当子模块的事情做完之后就回到主界面,或许还同时返回一些子模块完成的数据交给主Activity处理。这样的数据交流就要用到回调函数onActivityResult。
<1>startActivityForResult(Intent intent, int requestCode);
第一个参数:一个Intent对象
第二个参数:如果> = 0,当Activity结束时requestCode将归还在onActivityResult()中。以便确定返回的数据是从哪个Activity中返回
<2>onActivityResult(int requestCode, int resultCode, Intent data)
第一个参数:这个整数requestCode提供给onActivityResult,是以便确认返回的数据是从哪个Activity返回的。
这个requestCode和startActivityForResult中的requestCode相对应。
第二个参数:这整数resultCode是由子Activity通过其setResult()方法返回。
第三个参数:一个Intent对象,带有返回的数据。
<3>setResult(int resultCode, Intent data)
调用这个方法把Activity想要返回的数据返回到父Activity
第一个参数:当Activity结束时resultCode将归还在onActivityResult()中,一般为RESULT_CANCELED , RESULT_OK。
第二个参数:一个Intent对象,返回给父Activity的数据。
本实例一共有三个Activity。点击飞往火星跳转到otherActivity,把地球带给火星的消息显示在otherActivity中。点击返回地球时把火星带给地球的消息传到主Activity.
同理:
点击飞往月球跳转到thirdActivity,把地球带给月球的消息显示在thirdActivity中。点击返回地球时把月球带给地球的消息传到主Activity.
具体实现:
主Activity:
package xiaosi.onActivityResult; 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; import android.widget.TextView; public class OnActivityResultActivity extends Activity { private Button button = null; private Button button1 = null; private TextView text = null; private static final int Mars = 0; private static final int Moon = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); text = (TextView) findViewById(R.id.text); button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(OnActivityResultActivity.this, otherActivity.class); String content = "地球来的消息:你好,我是来自地球上的小老鼠。我好想去你们的火星呀"; intent.putExtra("FromEarth", content); startActivityForResult(intent, Mars); } }); button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(OnActivityResultActivity.this, thirdActivity.class); String content = "地球来的消息:你好,我是来自地球上的小老鼠。我好想去你们月球"; intent.putExtra("FromEarth", content); startActivityForResult(intent, Moon); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case Mars: Bundle MarsBuddle = data.getExtras(); String MarsMessage = MarsBuddle.getString("FromMars"); text.setText(MarsMessage); break; case Moon: Bundle MoonBuddle = data.getExtras(); String MoonMessage = MoonBuddle.getString("FromMoon"); text.setText(MoonMessage); break; } } }
otherActivity:
package xiaosi.onActivityResult; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class otherActivity extends Activity { private Button button = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.other); Intent EarthIntent = getIntent(); String EarthMessage = EarthIntent.getStringExtra("FromEarth"); button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(otherActivity.this, OnActivityResultActivity.class); String passString = "火星来的消息:Hello,我是火星的Jack,非常高兴你能来火星"; intent.putExtra("FromMars", passString); setResult(RESULT_OK, intent); finish(); } }); TextView textView = (TextView) findViewById(R.id.othertext); textView.setText(EarthMessage); } }
thirdActivity:
package xiaosi.onActivityResult; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class thirdActivity extends Activity { private Button button = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.third); Intent EarthIntent = getIntent(); String EarthMessage = EarthIntent.getStringExtra("FromEarth"); button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(thirdActivity.this, OnActivityResultActivity.class); String passString = "月球来的消息:Hello,我是月球的Lucy,非常欢迎你来月球"; intent.putExtra("FromMoon", passString); setResult(RESULT_OK, intent); finish(); } }); TextView textView = (TextView) findViewById(R.id.thirdtext); textView.setText(EarthMessage); } }