标签:button Activity Intent import startActivityForResult setResult intent android
主Activity)上能连接往许多不同子功能模块(子Activity上去),当子模块的事情做完之后就回到主界面,或许还同时返回一些子模块完成的数据交给主Activity处理。这样的数据交流就要用到
主Activity
回调函数onActivityResult()。
一些方法:
主
Activity中:
<1>主
Activity里面传递一个Intent对象,给即将打开的子
Activity,并用requestCode区分打开哪个 子
Activity
startActivityForResult(IntentrequestCode); |
第一个参数Intent intent: 一个Intent对象
第二个参数 int requestCode:>= 0,当 主Activity结束时 requestCode将归还在onActivityResult()中。以便确定返回的数据是从哪个Activity中返回
|
<2>
主
Activity
里重写这个方法,接收
子
Activity
收传入的
Intent的对象,
主
Activity既可以通过switch/
case语句用
requestCode
子
Activity,
也可以通过resultCode
来区分
onActivityResult(int requestCode, int resultCode, Intent data) |
int requestCode: 这个整数requestCode提供给onActivityResult,是以便确认返回的数据是从哪个Activity返回的。这个requestCode
和startActivityForResult中的requestCode相对应。
int resultCode: 由子Activity通过其setResult()方法返回。
Intent data
|
子
Activity中:
<3>
子
Activity
调用这个方法把数据返回到
主
Activity
,同时也返回区分自己的resultCode
setResult(int resultCode, Intent data) |
int resultCode: 当子Activity结束时resultCode将归还在主Activity的onActivityResult()中,一般为RESULT_CANCELED , RESULT_OK。 Intent data: 一个Intent对象,返回给父Activity的数据。 |
1 --》飞往火星
2--》返回地球
3--》从地球飞往月球
4---》返回地球
本实例一共有三个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)//重写 onActivityResult方法
{
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);
}
}
|
标签:button,
Activity,
Intent,
import,
startActivityForResult,
setResult,
intent,
android
From: https://blog.51cto.com/u_4387387/6951227