ANDROID笔记:Activity之间的传值

在主activty中调用

        Intent intent = new Intent(MyActivity.this, MyResultActivity.class);
        // 调用返回
        startActivityForResult(intent, 1);

并且重载下列方法:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // String nameString = data.getStringExtra("name");
        // String sexString = data.getStringExtra("sex");
        // 获取Person对象
        Person person = (Person) data.getSerializableExtra("person");
        if (person != null) {
            String name = person.getName();
            String sexString = person.getSex();
            Toast.makeText(getApplicationContext(), name + "---" + sexString,
                    Toast.LENGTH_SHORT).show();
        }

    }

在调用的另一个actvity中调用下列方法就会返回

Intent intent = new Intent(MyResultActivity.this, MyActivity.class);
        // intent.putExtra("name", "eve");
        // intent.putExtra("sex", "man");
        // 返回对象
        Person person = new Person("eve", "man");
        intent.putExtra("person", person);
        // 返回数据
        setResult(1, intent);
        finish();

 使用Intent传输的对象必须实现Serializable接口!

posted @ 2013-10-29 10:53  skyko  阅读(213)  评论(0编辑  收藏  举报