passing object to activity

前端 未结 3 1427
暗喜
暗喜 2021-01-16 20:39

Can i initialize object in my first activity and you it in all activity???

public class Calc{
    int x;
    int y;
    public Calc(int x, int y) {
       th         


        
3条回答
  •  悲哀的现实
    2021-01-16 21:07

    make

    public class Calc implements Serializable {
    int x;
    int y;
    public Calc(int x, int y) {
       this.x = x;
       this.y = y;
    }
    public int sum() {
        return x + y;
    }
    

    }

    and

    When you start your PreviewActivity

    Intent intent = new Intent(mContext, PreviewActivity .class);
    intent.putParcelableArrayListExtra("data",c);
    startActivity(intent);
    

    and in your PreviewActivity do this

    Calc c=(Calc ) getIntent().getSerializableExtra("data");
    

提交回复
热议问题