Created
December 21, 2017 09:14
-
-
Save SalaSuresh/2c693ce1c8d39590d67ad506b6cd0805 to your computer and use it in GitHub Desktop.
Passing object using one Activity to Other Activity using Intent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Contact implements Serializable { | |
public String name; | |
public String number; | |
public String getName() { | |
return name; | |
} | |
public String getNumber() { | |
return number; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public void setNumber(String number) { | |
this.number = number; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
Intent intent = new Intent(this, SecondActivity.class); | |
Contact contact = new Contact(); | |
contact.setName("User Name"); | |
contact.setNumber("0123456789"); | |
Log.e("test", "MainActivity: Name: " + contact.name + " Number: " + contact.number); | |
intent.putExtra("contact", contact); | |
startActivity(intent); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SecondActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
Toast.makeText(this, "" + getLocalClassName(), Toast.LENGTH_SHORT).show(); | |
Intent intent = getIntent(); | |
Contact contact = (Contact) intent.getSerializableExtra("contact"); | |
Log.e("test", "SecondActivity: Name: " + contact.name + " Number: " + contact.number); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment