passing object to activity

前端 未结 3 1428
暗喜
暗喜 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

    How can i pass it to another activity or how can i make it share with other activity

    Just use Intent so:

    intent.putExtra("calc" new Calc(3, 4));
    

    for passing this Object when you want to start another Activity.

    Then in your second Activity just call:

    Calc c = getIntent().getParcelableExtra("calc"); // getSerializableExtra("calc");
    

    And your Calc class have to implement Parcelable or Serializable

    public class Calc implements Parcelable { 
     ...
    }
    

提交回复
热议问题