0

I new a object int avtivity1.I want transmit it to activity2.And only transmit reference,not copy it.I need how to do?

public class MyActivity1 extends Activity{
public void onCreate(Bundle saveInstanceState){
    super.onCreate(saveInstanceState);
    setContentView(R.layout.main);

    myButton1 = (Button)findViewById(R.id.ButtonId1);
    myButton1.setText("goto");
    myButton1.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v){

            MyObject myObject = new MyObject();
            //how to transmit the object reference to OtherActiviy
            //I want only transmit,not copy the object

            Intent intent = new Intent();
            intent.setClass(MyActivity1.this, OtherActiviy.class);
            startActivity(intent);
        }
    });
}

}

shmosel
  • 49,289
  • 6
  • 73
  • 138
Getname
  • 78
  • 8

2 Answers2

0

You can put your Object which extend Parcelable or Serialization into intent. Or a bad way is parse object to String by Gson on Activity1 and convert String to object on Activity2.

Brian Hoang
  • 1,111
  • 9
  • 18
-1

You can do it this way :

  public class MyActivity1 extends Activity{
  public void onCreate(Bundle saveInstanceState){
    super.onCreate(saveInstanceState);
    setContentView(R.layout.main);

    myButton1 = (Button)findViewById(R.id.ButtonId1);
    myButton1.setText("goto");
    myButton1.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v){


             MyObject myObject = new MyObject();
            //how to transmit the object reference to OtherActiviy
            //I want only transmit,not copy the object

            Intent intent = new Intent();
            intent.putExtra("MYObject",myObject);
            intent.setClass(MyActivity1.this, OtherActiviy.class);
            startActivity(intent);
        }
    });
}

Note: myObject should be parcelable.