主要功能:android
startActivityForResult(Intent intent, int requestCode); |
第一個參數Intent intent: 一個Intent對象app
第二個參數
int requestCode
:>= 0,
當主Activity結束時requestCode
將歸還在onActivityResult()中。以便肯定返回的數據是從哪一個Activity中返回
|
onActivityResult(int requestCode, int resultCode, Intent data) |
int requestCode: 這個整數requestCode提供給onActivityResult,是以便確認返回的數據是從哪一個Activity返回的。
這個requestCode
和
startActivityForResult中的requestCode相對應。
int resultCode: 由子Activity經過其setResult()方法返回。 ide |
setResult(int resultCode, Intent data) |
int resultCode: 當子Activity結束時resultCode將歸還在主Activity的onActivityResult()中,通常爲RESULT_CANCELED , RESULT_OK。this Intent data: 一個Intent對象,返回給父Activity的數據。spa |
同理:.net
地球--》飛往月球 月球---》返回地球 對象
具體實現:blog
主Activity:ci
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:get
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);
}
}
|