---
title: "Examples of Intent and Bundles"  
description: "Examples of Intent and Bundles"  
author: "Allen Scott"  
published: 2016-01-18  
updated: 2023-06-27  
canonical: https://www.mindstick.com/forum/33890/examples-of-intent-and-bundles  
category: "android"  
tags: ["android", "android intent"]  
reading_time: 2 minutes  

---

# Examples of Intent and Bundles

I need a [simple](https://yourviews.mindstick.com/story/1469/5-simple-ways-to-stay-fit-amp-healthy) example [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) that will show me the [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) between [Intents](https://www.mindstick.com/interview/2590/describe-intents-in-detail) and Bundles

## Replies

### Reply by Aryan Kumar

Sure, here is an example of how to use an Intent and a Bundle to transfer data from one activity to another:

Code snippet

```plaintext
// Create an Intent to start the second activity.
Intent intent = new Intent(this, SecondActivity.class);

// Add some data to the Intent.
Bundle bundle = new Bundle();
bundle.putString("data", "This is the data");
intent.putExtras(bundle);

// Start the activity.
startActivity(intent);
```

In the second activity, you can retrieve the data that was passed in the Intent using the `getExtras()` method.

Code snippet

```plaintext
// Get the data from the Intent.
Bundle bundle = getIntent().getExtras();
String data = bundle.getString("data");
```

The `data` variable will now contain the string that was passed in the Intent.

Here is an example of how to use a Bundle to store a more complex data structure:

Code snippet

```plaintext
// Create a Bundle to store the data.
Bundle bundle = new Bundle();

// Add a person object to the Bundle.
Person person = new Person();
person.setName("John Doe");
person.setAge(30);
bundle.putSerializable("person", person);

// Start the activity.
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtras(bundle);
startActivity(intent);
```

In the second activity, you can retrieve the person object from the Bundle using the `getSerializable()` method.

Code snippet

```plaintext
// Get the person object from the Bundle.
Bundle bundle = getIntent().getExtras();
Person person = (Person) bundle.getSerializable("person");
```

The `person` variable will now contain the person object that was passed in the Bundle.

### Reply by Anonymous User

In MainActivity :

```
Intent intent = new Intent(this, OtherActivity.class);intent.putExtra(OtherActivity.KEY_EXTRA, yourDataObject);startActivity(intent);
```

\
In OtherActivity :

```
public static final String KEY_EXTRA = "com.example.yourapp.KEY_BOOK";@Overrideprotected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  String yourDataObject = null;  Bundle extras = getIntent().getExtras();  if (extras != null) {      yourDataObject = extras.getString(KEY_EXTRA);  }  if (yourDataObject != null) {        // do stuff  } else {      throw new IllegalArgumentException("Activity cannot find  extras " + KEY_EXTRA);  }}
```

More informations here : http://developer.android.com/reference/android/content/Intent.html


---

Original Source: https://www.mindstick.com/forum/33890/examples-of-intent-and-bundles

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
