记录activity之间的数据传递

版权声明:本文为博主原创文章,未经博主允许不得转载。

方式一:通过Intent传递实体的方法,直接进行传递

来源页:

    //传递数据,进行跳转
     Intent intent = new Intent(MyCarListActivity.this, ModifyMyCarTypeActivity.class);
    //使用intent上挂可序列化实体的方式传递实体
    intent.putExtra(IntentPostConstants.MyCarListEntity, myCarListEntity);
    startActivity(intent);

目标页:

        /获取数据
       if(getIntent()!=null){
            MyCarListEntity myCarListEntity= (MyCarListEntity) getIntent()
                    .getSerializableExtra(IntentPostConstants.MyCarListEntity);
            if (myCarListEntity != null) {
                carBrand.setText(myCarListEntity.getTypeName());
                maycar_type_carnumber.setText(myCarListEntity.getPlateNumber());
                ID = myCarListEntity.getID();
            }
        }

方式二:使用Bundle传递序列化对象

来源页:

     //传递数据,进行跳转
     Intent intent = new Intent();
     Bundle bundle = new Bundle();
     bundle.putSerializable(IntentPostConstants.MyCarListEntity, myCarListEntity);
     intent.putExtras(bundle);
     intent.setClass(MyCarListActivity.this, ModifyMyCarTypeActivity.class);
     startActivity(intent);

目标页:

  //获取intent值
        if (getIntent() != null) {

            Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                MyCarListEntity myCarListEntity = (MyCarListEntity)
                        bundle.getSerializable(IntentPostConstants.MyCarListEntity);
                if (myCarListEntity != null) {
                    carBrand.setText(myCarListEntity.getTypeName());
                    maycar_type_carnumber.setText(myCarListEntity.getPlateNumber());
                    ID = myCarListEntity.getID();
                }
            }


        }

声明intent传递常量:

/**
 * Created by zhang on 2016/7/31.
 * <p>
 * intent数据传递常量类
 */
public class IntentPostConstants {

    public final static String MyCarListEntity = "MyCarListEntity"; //我的汽车信息实体


}

对象需要序列化:

public class MyCarListEntity implements Serializable {
 private String ID;//自增列
 private String BuyTime;//买车时间
 private String CarModelId;//车型Id
 ……………………
 }

startActivityForResult的数据传递:

来源页:

 Intent intent = new Intent(MyCarListActivity.this, ModifyMyCarTypeActivity.class);
     //使用intent上挂可序列化实体的方式传递实体
      intent.putExtra(IntentPostConstants.MyCarListEntity, myCarListEntity);
      startActivityForResult(intent, 8);

执行的回调:
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
   switch (requestCode) {
      case 8:

       if (resultCode == 6 && data != null) {
          CarMakeListEntity carMakeListEntity = (CarMakeListEntity)
         data.getSerializableExtra(IntentPostConstants.CarMakeListEntity);

         carBrand.setText(carMakeListEntity.getTypeName());


                }
        }
    }

目标页:

                CarMakeListEntity entity = (CarMakeListEntity) data;
                Intent intent = new Intent();
                intent.putExtra(IntentPostConstants.CarMakeListEntity, entity);
                setResult(6, intent);
                finish();

猜你喜欢

转载自blog.csdn.net/zr940326/article/details/52077990
今日推荐