Android pass argument to another activity

Android pass argument to another activity

Just Wanted to save this snippet quickly so I can share it. more description will follow later.

Set the variable inside the List Activity

// Inside Event List Activity
//----------------------------
// We pass an ID to Event Detail Activity

Intent intent = new Intent(getBaseContext(), EventDetailActivity.class);
intent.putExtra(EventDetailActivity.ARG_EVENT_ID, eventlist.get(position).getId());
startActivity(intent);

Get the variable inside the Detail Activity

// Inside Event Detail Activity 
// ---------------------------

// Define The static Variable that will be used as a Key by both the List and Detail activity
static final String ARG_EVENT_ID = "event_id";

//.. Later in onCreate
protected void onCreate(Bundle savedInstanceState) {

// Get the variable that was set in the List Activity
// Set it as -1 if its undefined

Intent intent = getIntent();
final int id = intent.getIntExtra(ARG_EVENT_ID,-1);


Hope you found this useful,

Thanks, Lehel