欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android activity之间数据传递更新UI(一)

程序员文章站 2022-06-01 14:36:06
...

正统方法

  1. 单行数据
 // 传值
        Intent intent = new Intent(this, KapHomePageActivity.class);
        intent.putExtra("homePageActivity_name","小明");
 // 取值
        Intent intent =  getIntent();
        String name = intent.getStringExtra("homePageActivity_name");
  1. 多行数据
// 传值
        Intent intent = new Intent(this, KapHomePageActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("name","小明");
        bundle.putInt("age",21);
        intent.putExtra("homePageActivity_bundle",bundle);
// 取值
        Intent intent =  getIntent();
        Bundle bundle = intent.getBundleExtra("homePageActivity_bundle");
        String name =  bundle.getString("name");
        int age = bundle.getInt("age");
  1. 对象(需要将对象进行序例化: Serializable)
// 序列化对象
public abstract class KapModelBase implements Serializable{ // 实现序列化接口,用于页面间传递对象
}
// 传值
        Intent intent = new Intent(this, KapHomePageActivity.class);
        intent.putExtra("homePageActivity_modelBill", new KapModelBill());
// 取值
        Intent intent =  getIntent();
        KapModelBill bill = (KapModelBill)intent.getSerializableExtra("homePageActivity_modelBill");
  1. 数组 (数组元素要实现Parcelable接口)
// 传值
        Intent intent = new Intent(this, KapHomePageActivity.class);
        intent.putParcelableArrayListExtra("homePageActivity_modelArray", new ArrayList<KapModelBill>());

// 取值
        Intent intent =  getIntent();
        ArrayList<KapModelBill> modelList = intent.getParcelableArrayListExtra("homePageActivity_modelArray");