Passing data with Intent

Intent is used to invoke another activity in Android development. Its always good if we can send some information along with Intent as we can make decisions based on the data received with an Intent.

For the Sri Lankan Holidays 2011 app I developed, I used this method to send data with Intent. So when user selects a month, it invokes another activity via Intent. When the activity is invoked, I can figure out which month user selected if I can send some information about month with the Intent.

Check out this example.

Sending Information “Jan” while starting the activity Jan.

Intent Jan = new Intent(Months.this,Holiday.class);
	            	Jan.putExtra("ListCount", "Jan");
	            	startActivity(Jan);

catching the data sent

Bundle extras = getIntent().getExtras();
		String data = extras.getString("ListCount");
		
		if(data.equals("Jan")){
			TextView txtMonth = (TextView)findViewById(R.id.txtMonth);
			txtMonth.setText("January");
			
			TextView txtDetail = (TextView)findViewById(R.id.txtDetail);
			txtDetail.setText(R.string.JanHol);
		}

Hope this small code snippet helps you out in this regard. :)

Did you enjoy this article?
Signup today and receive free updates straight in your inbox. We will never share or sell your email address.

Leave a Reply