安卓Activity之间的数据传递四种方法,以及数据回传的实现
显式Intent来跳转页面和传递数据:
Intent intent=new Intent(MainActivity.this,ReceiverActivity.class);
intent.putExtra("显式数据","显示意图Intent传送数据");
startActivity(intent);
接受数据时:
TextView txtshow=(TextView)findViewById(R.id.txtShow);
Intent intent =getIntent();
txtshow.setText(intent.getStringExtra("显式数据"));
System.out.println(intent.getStringExtra("显式数据"));
隐式Intent
来执行一个动作,这个动作可以是系统自己定义的,比如发短信,打电话等等,action可以在AndroidManifest.xml中定义
<activity android:name=".ReceiverActivity">
<intent-filter>
<action android:name="ReceiveData"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</activity>
使用时:
Intent intent =new Intent("ReceiveData");//为Intent添加动作
intent.addCategory("android.intent.category.DEFAULT");
//Category定义当前动作(Action)被执行的环境,如果不符合这个环境将无法**,如,CATEGORY_DEFAULT
,默认环境,所有intent都能**
startActivity(intent);//很奇怪,只会跳转到某个没有控件的界面
Bundle作为媒介:
Bundle bundle=new Bundle();
Intent intent =new Intent(MainActivity.this,ReceiverActivity.class);
bundle.putString("Bundle","Bundle传输数据");
intent.putExtra("Message",bundle);
startActivity(intent);
Serializable:
我们要创建一个类,并且让这个类(执行)implements Serializable
如:
package com.example.sendtypes;
import java.io.Serializable;
public class Serial implements Serializable {
public int getUserno() {
return userno;
}
public void setUserno(int userno) {
this.userno = userno;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
private int userno;
private String username;
}
接受数据代码:
Bundle bundle=getIntent().getBundleExtra("Message"); Bundle的代码和序列化的代码不可以同时运行,所以运行哪一个就把哪一个给注释
txtshow.setText(bundle.getString("Bundle"));
使用时:
Serial serial=new Serial();
serial.setUsername(“张三”);
serial.setUserno(001);
Intent intent=new Intent(MainActivity.this,ReceiverActivity.class);
intent.putExtra(“序列化”,serial);
startActivity(intent);
接受数据时:
Serial serial=(Serial)getIntent().getSerializableExtra("序列化");
txtshow.setText("序列化显示:\n 用户名:"+serial.getUsername());
数据回传的实现
数据回传指的是,你可以在活动一中用intent传输数据给活动二,并且定义一个请求码,这个请求码是区分哪个到底是哪个活动发起的数据回调,因为很多个活动都要求数据回调时,系统会分不清数据到底是给哪一个,我们在活动二里面也要定义一个结果码,它是为了区分到底是哪一个回调数据,因为活动二可能会回调很多个数据给活动一,如果没有结果码,那么活动一无法区分到底结果。(注意,发起回调的活动同时可以给返回结果的活动传输数值)
下面给代码:
请求回传的活动:
Intent intent =new Intent(MainActivity.this,ReturnActivity.class);//实现页面跳转
Bundle bundle=new Bundle();//定义一个Bundle装入数值
bundle.putString("name","小洪言");
bundle.putInt("age",20);
intent.putExtra("grilfriend",bundle);//将Bundle装入intent里面
startActivityForResult(intent,1);//要想得到数据回传就i必须要用startActivityForResult
第一个参数是我们定义的intent,第二个参数是请求码(request code)
回传数据的活动:
TextView txtshow2=(TextView)findViewById(R.id.txtShow2);
Intent intent=getIntent();
txtshow2.setText("姓名:"+intent.getBundleExtra("grilfriend").getString("name")+"\n年龄:"+intent.getBundleExtra("grilfriend").getInt("age"));
intent.putExtra("boyfriend","何佳洋");
setResult(1,intent);//第一个参数是结果码,区分到底是哪一个
然后第三步,我们要在发起请求数据回调的活动写一个方法,来操作返回的数据:
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1)
{
if(resultCode==1)
{
TextView txtshow=(TextView)findViewById(R.id.txtReturn);
String s;
s=data.getStringExtra("boyfriend");
System.out.println(s);
txtshow.setText(s);
}
}
}
下面给出总代码:
MainActivity.java:
package com.example.sendtypes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnintentxian=(Button)findViewById(R.id.btnIntentxian);
Button btnintentyin=(Button)findViewById(R.id.btnIntentyin);
Button btnbundle=(Button)findViewById(R.id.btnBundle);
Button btnserializable=(Button)findViewById(R.id.btnSerializable);
Button btnreturn=(Button)findViewById(R.id.btnReturn);
btnintentxian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,ReceiverActivity.class);
intent.putExtra("显式数据","显示意图Intent传送数据");
startActivity(intent);
}
});
btnintentyin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent =new Intent("ReceiveData");//为Intent添加动作
intent.addCategory("android.intent.category.DEFAULT");
//Category定义当前动作(Action)被执行的环境,如果不符合这个环境将无法**,如,CATEGORY_DEFAULT ,默认环境,所有intent都能**
startActivity(intent);//很奇怪,只会跳转到某个没有控件的界面
}
});
btnbundle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle=new Bundle();
Intent intent =new Intent(MainActivity.this,ReceiverActivity.class);
bundle.putString("Bundle","Bundle传输数据");
intent.putExtra("Message",bundle);
startActivity(intent);
}
});
btnserializable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Serial serial=new Serial();
serial.setUsername("张三");
serial.setUserno(001);
Intent intent=new Intent(MainActivity.this,ReceiverActivity.class);
intent.putExtra("序列化",serial);
startActivity(intent);
}
});
btnreturn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent =new Intent(MainActivity.this,ReturnActivity.class);
Bundle bundle=new Bundle();
bundle.putString("name","小洪言");
bundle.putInt("age",20);
intent.putExtra("grilfriend",bundle);
startActivityForResult(intent,1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1)
{
if(resultCode==1)
{
TextView txtshow=(TextView)findViewById(R.id.txtReturn);
String s;
s=data.getStringExtra("boyfriend");
System.out.println(s);
txtshow.setText(s);
}
}
}
}
ReceiverActivity.java:
package com.example.sendtypes;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ReceiverActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receiver);
TextView txtshow=(TextView)findViewById(R.id.txtShow);
// Intent intent =getIntent();
// txtshow.setText(intent.getStringExtra(“显式数据”));
// System.out.println(intent.getStringExtra(“显式数据”));
//Bundle///
// Bundle bundle=getIntent().getBundleExtra(“Message”); Bundle的代码和序列化的代码不可以同时运行,所以运行哪一个就把哪一个给注释
// txtshow.setText(bundle.getString(“Bundle”));
Serializable//
// Serial serial=(Serial)getIntent().getSerializableExtra(“序列化”);
// txtshow.setText(“序列化显示:\n 用户名:”+serial.getUsername());
}
}
ReturnActivity.java:
package com.example.sendtypes;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class ReturnActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_return);
TextView txtshow2=(TextView)findViewById(R.id.txtShow2);
Intent intent=getIntent();
txtshow2.setText("姓名:"+intent.getBundleExtra("grilfriend").getString("name")+"\n年龄:"+intent.getBundleExtra("grilfriend").getInt("age"));
intent.putExtra("boyfriend","何佳洋");
setResult(1,intent);
}
}
Serial.java:
package com.example.sendtypes;
import java.io.Serializable;
public class Serial implements Serializable {
public int getUserno() {
return userno;
}
public void setUserno(int userno) {
this.userno = userno;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
private int userno;
private String username;
}