Android Java向另一个页面跳转时使用bundle传递一个对象
在Android Java中,可以使用Bundle传递一个对象到另一个页面。以下是一些步骤:
- 在要传递的对象类中实现Serializable接口,例如:
public class MyObject implements Serializable { //对象的属性和方法 }
- 在当前页面中创建一个Bundle对象,将要传递的对象放入Bundle中,例如:
MyObject myObject = new MyObject(); Bundle bundle = new Bundle(); bundle.putSerializable("my_object", myObject);
- 使用Intent跳转到另一个页面,并将Bundle对象传递给Intent,例如:
Intent intent = new Intent(this, AnotherActivity.class); intent.putExtra("my_bundle", bundle); startActivity(intent);
- 在另一个页面中获取传递的Bundle对象和其中的对象,例如:
Bundle bundle = getIntent().getBundleExtra("my_bundle"); MyObject myObject = (MyObject) bundle.getSerializable("my_object");
注意:传递的对象必须实现Serializable接口,否则会出现异常。并且,传递的对象不能太大,否则会导致性能问题
原文地址: https://www.cveoy.top/t/topic/cwGU 著作权归作者所有。请勿转载和采集!