Created
March 14, 2015 13:48
-
-
Save bdiegel/d7f8d14789dedf1d09ef to your computer and use it in GitHub Desktop.
Android Parcelables with Intents
This file contains hidden or 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
/** | |
* Creating and passing a Parcelable with an Intent | |
*/ | |
// Creating an instance of Student class with user input data | |
Student student = new Student(mNameTV.getText().toString(), | |
Integer.parseInt(mAgeTV.getText().toString()), | |
mAddressTV.getText().toString(), | |
mCourseTV.getText().toString()); | |
// Creating an intent to open the activity StudentViewActivity | |
Intent intent = new Intent(getBaseContext(), StudentViewActivity.class); | |
// Passing data as a parecelable object to StudentViewActivity | |
intent.putExtra("student",student); | |
// Opening the activity | |
startActivity(intent); | |
/** | |
* Receiving a Parcelable in started Actvitiy | |
*/ | |
// Fetching data from a parcelable object passed from MainActivity | |
Student student = getIntent().getParcelableExtra("student"); | |
/** | |
* Implmenting Parcelable interface: | |
* http://www.parcelabler.com/ | |
* http://developer.android.com/reference/android/os/Parcelable.html | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment