package com.fmyboke; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.R.integer; import android.R.string; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.SimpleAdapter; import android.widget.Spinner; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 第一个参数 上下文 // 要跳转的activity 的class对象 Intent intent = new Intent(this, Second.class); // 可以传string[] string int[] int ..... // 对象需要序列化 intent.putExtra("name", "张三"); intent.putExtra("nameArray", new String[] { "张三", "李四" }); //创建一个序列化对象 User user = new User(); user.setAge(18); user.setName("范冰冰"); intent.putExtra("user", user); //使用bundle传值 Bundle bundle = new Bundle(); // 可以传string[] string int[] int ..... // 对象需要序列化 bundle.putString("name", "张三"); bundle.putStringArray("nameArray", new String[] { "张三", "李四" }); bundle.putSerializable("user", user); //放入意图中 intent.putExtra("bundle", bundle); //开始跳转至 startActivity(intent); } static class User implements Serializable{ int age; String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } } }
跳转的Activity:
package com.fmyboke; import java.io.Serializable; import com.fmyboke.MainActivity.User; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class Second extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.second); //得到意图 Intent intent = getIntent(); //获取字符串 String name = intent.getStringExtra("name"); //获取字符串数组 String[] nameArray = intent.getStringArrayExtra("nameArray"); //得到一个序列化对象 User user =(User) intent.getSerializableExtra("user"); //获取bundle Bundle bundle = intent.getBundleExtra("bundle"); //得到一个字符串 String nameBundle = bundle.getString("name"); //得到一个字符串数组 String[] nameArrayBundle = bundle.getStringArray("nameArray"); //得到一个序列化对象 Serializable userBundle = bundle.getSerializable("user"); } }