Skip to content

Instantly share code, notes, and snippets.

@bdiegel
Created March 14, 2015 13:48
Show Gist options
  • Save bdiegel/d7f8d14789dedf1d09ef to your computer and use it in GitHub Desktop.
Save bdiegel/d7f8d14789dedf1d09ef to your computer and use it in GitHub Desktop.
Android Parcelables with Intents
/**
* 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