安卓activity之间进行数据传输

*爱你&永不变心* 提交于 2020-03-10 19:36:46

一:单个对象传输

(1)基本数据传输(String Int)

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("name","android");
intent.putExtra("version",9);
startActivity(intent);

在另外一个NewActivity.java的onCreate获取

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);

        Intent intent = getIntent();
        String name =  intent.getStringExtra("name");
        int ver = intent.getIntExtra("version",0);
        Log.d(TAG," name ="+name +" version="+ver);
    }

(2)Bundle集装箱数据传输
传递方:

Intent intent = new Intent(this, NewActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name","android");
bundle.putInt("version",9);
intent.putExtra("bundle_name",bundle);
startActivity(intent);

接收方:

Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("bundle_name");
String name = bundle.getString("name");
int ver = bundle.getInt("version",0);
Log.d(TAG,"name ="+name +" version="+ver);

(3)传输单个对象,使用Serializable
创建一个实现Serializable对象的类名叫AndroidInfo

import java.io.Serializable;

public class AndroidInfo implements Serializable {
    private String name;
    private int version;

    public void setName(String name) {
        this.name = name;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public String getName() {
        return name;
    }

    public int getVersion() {
        return version;
    }
}

发送方:

Intent intent = new Intent(this, NewActivity.class);
AndroidInfo info = new AndroidInfo();
info.setName("android");
info.setVersion(10);
intent.putExtra("infoName",info);
startActivity(intent);

接收方:

AndroidInfo info = (AndroidInfo)getIntent().getSerializableExtra("infoName");
Log.d(TAG,"--> name = "+info.getName()+" ver="+info.getVersion());

(4)对象集合数据传输
AndroidInfo类如上(3)
public class AndroidInfo implements Serializable {
…//实现内容如上(3)AndroidInfo类
}

发送方

   ArrayList<AndroidInfo> infoList = new ArrayList<AndroidInfo>();
    Intent intent = new Intent(this, NewActivity.class);
    AndroidInfo info = new AndroidInfo();
    info.setName("android");
    info.setVersion(10);
    infoList.add(info);

    AndroidInfo info1 = new AndroidInfo();
    info1.setName("android");
    info1.setVersion(11);
    infoList.add(info1);

    intent.putExtra("infoListName",infoList);
    startActivity(intent);

接送方:

   List<AndroidInfo> info = (List<AndroidInfo>)getIntent().getSerializableExtra("infoListName");
    for(int i=0;i<info.size();i++) {
        Log.d(TAG, "--> name = " + info.get(i).getName() + " ver=" + info.get(i).getVersion());
    }

(5)activity之间回传数据
(1)在activity a中跳转到NewActivity

    private static final int REQUEST_CODE = 1;
    private static final int RESULT_CODE = 2;

   Intent intent = new Intent(this, NewActivity.class);
   startActivityForResult(intent,REQUEST_CODE);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == REQUEST_CODE && resultCode == RESULT_CODE){
       String name = data.getStringExtra("name");
       Log.d(TAG,"--->  name ="+name);
    }
}

在activity b(名为NewActivity)中,退出的时候

    Intent intent = new Intent();
    intent.putExtra("name","android10");
    setResult(RESULT_CODE,intent);
    NewActivity.this.finish();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!