Explain the use of putExtra() and getExtra() and their associated arguments for passing data from one activity to another in an Android Application. Support your answer with an illustrative example for each method.
putExtra() is a method in Android that allows you to pass data from one activity to another. It is used to attach extra data to an Intent object, which is then used to start another activity. The putExtra() method takes two arguments: a key and a value. The key is a string that acts as a unique identifier for the data, and the value is the actual data that you want to pass.
For example, let's say we have two activities: Activity A and Activity B. In Activity A, we want to pass a string value to Activity B. We can do this using putExtra() as follows:
```java
// In Activity A
String message = "Hello, Activity B!";
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("key_message", message);
startActivity(intent);
```
In the above example, we create a string variable called "message" and assign it the value "Hello, Activity B!". We then create an Intent object and use putExtra() to attach the message to it. The key we use is "key_message". Finally, we start Activity B using the intent.
To retrieve the data in Activity B, we use the getExtra() method. This method takes the same key as an argument and returns the value associated with that key.
```java
// In Activity B
Intent intent = getIntent();
String message = intent.getStringExtra("key_message");
```
In the above example, we retrieve the Intent object from the getIntent() method. We then use getExtra() with the key "key_message" to retrieve the string value that was passed from Activity A.
Overall, putExtra() and getExtra() are essential methods for passing data between activities in an Android application. They allow you to easily transfer information and communicate between different parts of your app.