两个activity之间传递数据用startActivityForResult方法。

时间:2023-03-09 02:03:47
两个activity之间传递数据用startActivityForResult方法。
 package com.example.testactivityresquest;

 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.Toast; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) { Intent intent = new Intent(MainActivity.this, Activityb.class);
int[] nums = { 15, 15 };
intent.putExtra(Changliang.KEY, nums);
//有别于startActivity,如果启动的其他Activity多了以后。相当于定一个特定KEY值,返回根据KEY值返回。
startActivityForResult(intent, Changliang.requestCode); }
});
}
//Activityb传回来的数据在这个方法中获取
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { int s = data.getIntExtra(Changliang.Activity_b_KEY, 1); Toast.makeText(getApplicationContext(), "传递两个数得到的和是:" + s, 1).show(); } }
 package com.example.testactivityresquest;

 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.Toast; public class Activityb extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityb); Intent intent = this.getIntent();
int[] n = intent.getIntArrayExtra(Changliang.KEY); final int nums = n[0] + n[1]; Toast.makeText(this, n[0] + " " + n[1], 1).show(); Button btn = (Button) findViewById(R.id.button2);
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) { Intent intent1 = new Intent(Activityb.this, MainActivity.class); intent1.putExtra(Changliang.Activity_b_KEY, nums);
// 将数据根据特定键值的意图事件导入
Activityb.this.setResult(Changliang.requestCode, intent1);
//关闭后返回主Activity
Activityb.this.finish(); }
});
} }
package com.example.testactivityresquest;

public class Changliang {

public static final String KEY="key";

public static final String Activity_b_KEY="key1";

public static final int requestCode=1987;

}

xml文件:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="88dp"
android:text="启动Activityb" /> </RelativeLayout>
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00"
android:orientation="vertical" > <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回Activity" /> </LinearLayout>

别忘在AndroidManifast中注册activityb。

运行效果图:

两个activity之间传递数据用startActivityForResult方法。