package com.sam.bundleobjectpass; import java.io.Serializable; /** * Created by Samir on 31.8.2016. */ public class Model implements Serializable { private String name; private String surName; private int age; public Model(String name, String surName, int age) { this.name = name; this.surName = surName; this.age = age; } public String getName() { return name; } public String getSurName() { return surName; } public int getAge() { return age; } }
MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Model model = new Model("Sam", "Sami",32); Intent i = new Intent(MainActivity.this, ReceiverActivity.class); i.putExtra("Editing", model); // sending our object startActivity(i); } }
ReceiverActivity.java
public class ReceiverActivity extends Activity { TextView txt_name; TextView txt_surname; TextView txt_age; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); txt_name = (TextView)findViewById(R.id.txt_name); txt_surname = (TextView)findViewById(R.id.txt_surname); txt_age = (TextView)findViewById(R.id.txt_age); // receiving our object Model model = (Model) getIntent().getSerializableExtra("Editing"); txt_name.setText(model.getName()); txt_surname.setText(model.getSurName()); txt_age.setText(""+model.getAge()); } }
References
https://stackoverflow.com/questions/5784231/how-to-pass-custom-object-in-bundle