在Android中经常需要从一个Activtity向另外一个Activity传递数值或对象.
首先要有一个类,这个类必须去implement Serializable;代码如下
public class Student implements Serializable{
String name;
}Intent intent = new Intent(MainActivity.this,SecondActivity.class);
Student s = new Student();
s.name = "wahaha";
intent.putExtra("stu", s);
startActivity(intent);textView = (TextView) findViewById(R.id.textview);
Intent intent = getIntent();
Serializable extra = intent.getSerializableExtra("stu");
Student s = new Student();
String sName = null;
//如果向下转型为Student类
if(extra instanceof Student){
//强制转型
sName = ((Student)extra).name;
}
textView.setText(sName);版权声明:本文为博主原创文章,未经博主允许不得转载。
Android 从一个Activity向另一个Activity传递一个对象
原文地址:http://blog.csdn.net/xiaoxiaomao123098/article/details/47747005