在一个activity中启动另一个activity并等待传输数据,即startActivityForResult()的使用

 

第一个activity -------------Main


package com.webabcd.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class Main extends Activity {

TextView txt;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);


txt = (TextView) this.findViewById(R.id.txt);
txt.setText("Activity 1");


Button btn = (Button) this.findViewById(R.id.btn);
btn.setText("启动另一个Activity");
btn.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v) {

// 实例化 Intent,指定需要启动的 Activity
Intent intent = new Intent();
intent.setClass(Main.this, MyActivity.class);


// 实例化 Bundle,设置需要传递的参数
Bundle bundle = new Bundle();
bundle.putString("name", "webabcd");
bundle.putDouble("salary", 100.13);


// 将需要传递的参数赋值给 Intent 对象
intent.putExtras(bundle);


// startActivity(intent); // 启动指定的 Intent(不等待返回结果)
// Main.this.finish();

// 启动指定的 Intent,并等待返回结果
// 其中第二个参数如果大于等于零,则返回结果时会回调 onActivityResult() 方法
startActivityForResult(intent, 0);

}
});

Log.d("MyDebug", "onCreate");
}

// 被启动的 Activity 返回结果时的回调函数
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK){
       Bundle bundle = data.getExtras();
       
       String name = bundle.getString("name");
       double salary = bundle.getDouble("salary");
       
       txt.setText("Activity 1" + "\n名字:" + name + "\n薪水:" + String.valueOf(salary));
}
}


@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();

Log.d("MyDebug", "onStart");
}


@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();

Log.d("MyDebug", "onStop");
}


@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();

Log.d("MyDebug", "onRestart");
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();

Log.d("MyDebug", "onPause");
}


@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();

Log.d("MyDebug", "onResume");
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();

Log.d("MyDebug", "onDestroy");
}

}


第二个activity ------------- MyActivity

package com.webabcd.activity;


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


// 被另一个 Activity 所启动的 Activity
public class MyActivity extends Activity {

Intent intent;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main2);


// 获取启动者传递过来的参数
intent = this.getIntent();
Bundle bundle = intent.getExtras();
String name = bundle.getString("name");
double salary = bundle.getDouble("salary");

TextView txt = (TextView) this.findViewById(R.id.txt);
txt.setText("Activity 2" + "\n名字:" + name + "\n薪水:" + String.valueOf(salary));


Button btn = (Button) this.findViewById(R.id.btn);
btn.setText("返回前一个Activity");
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// 返回参数给启动者
MyActivity.this.setResult(Activity.RESULT_OK, intent);
MyActivity.this.finish();
}
});
}
}


布局文件-------main1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/txt" />

<Button android:id="@+id/btn" android:layout_width="wrap_content"
android:layout_height="wrap_content" />


</LinearLayout>


布局文件----mian2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/txt" />

<Button android:id="@+id/btn" android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值