Activity 之间的数据转换

Intent putExtra()方法

示例,Activity A 跳转B
A:

    Intent intent = new Intent(MainActivity.this,SecActivity.class);
                intent.putExtra("name","charlie");
                startActivity(intent);

B:

       String data = getIntent().getStringExtra("name");
        ((TextView)findViewById(R.id.tv)).setText(data);

Bundle 传值

A:

  Intent intent = new Intent(MainActivity.this,SecActivity.class);

                Bundle bundle = new Bundle();
                bundle.putString("name","charlie");
                intent.putExtras(bundle);
                startActivity(intent);

B:

  Bundle bundle = getIntent().getExtras();
        String data = bundle.getString("name");
        ((TextView)findViewById(R.id.tv)).setText(data);

传递对象

示例
Person 对象:

public class Person implements Serializable{
    private String name;
    private String age ;

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "name=="+name+"::::"+"age==="+age;
    }

A:

  Intent intent = new Intent(MainActivity.this,SecActivity.class);
                Bundle bundle = new Bundle();
                Person person = new Person();
                person.setName("ccccc");
                person.setAge("777");
                bundle.putSerializable("person",person);
                intent.putExtras(bundle);
                startActivity(intent);

B:

   Person person = (Person)getIntent().getExtras().getSerializable("person");
        ((TextView)findViewById(R.id.tv)).setText(person.toString());

你可能感兴趣的:(Activity 之间的数据转换)