Student.java
package cn.itcast.wh08.multiactivity.domain; import java.io.Serializable; public class Student implements Serializable{ public String name; public int age; public Student(String name, int age) { super(); this.name = name; this.age = age; } }
MainActivity.java
package cn.itcast.wh08.multiactivity; import cn.itcast.wh08.multiactivity.domain.Student; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void jump(View v){ Intent intent = new Intent(this,OtherActivity.class);//意图对象 //对象 Student student = new Student("武藤兰", 40); intent.putExtra("student", student); startActivity(intent); } }
OtherActivity.java
package cn.itcast.wh08.multiactivity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; import cn.itcast.wh08.multiactivity.domain.Student; public class OtherActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_other); TextView tv_info = (TextView) findViewById(R.id.tv_info); Intent intent = getIntent();//获取激活自己的意图对象 //获取意图里面的数据 Student student = (Student) intent.getSerializableExtra("student"); String name = student.name; int age = student.age; tv_info.setText("name:"+name+",age:"+age); } }