BundleDemo.java ----主Activity从此Activity中传给 ActivityA.java
public class BundleDemo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bundle_activity);
User user = new User();
Intent intent = new Intent(this, ActivityA.class);
Bundle bundle = new Bundle();
bundle.putSerializable("user", user);
intent.putExtras(bundle);
startActivity(intent);
}
}
class User implements Serializable {
public ArrayList getList() {
ArrayList list = new ArrayList();
list.add("tian1");
list.add("tian2");
list.add("tian3");
list.add("tian4");
return list;
}
}
ActivityA.java--- 获得user类
public class ActivityA extends Activity {
private Button button;
private String Tag = "yuxlong2010";
int count = 0;
private TextView textview;
User user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(Tag, "I'm onCreate");
setContentView(R.layout.activity_a);
textview = (TextView) findViewById(R.id.textView1);
button = (Button) findViewById(R.id.button1);
Bundle bundle = this.getIntent().getExtras();
user = (User) bundle.get("user");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//startActivity(new Intent(ActivityA.this,ActivityA.class));
textview.setText(user.getList().get(0).toString());
}
});
}