When you start go from one activity to another, you often want to transfer some information across. Intents can be used to ferry this information to the new activity.

Picking up from where we last left intents, you can start a new activity like so:

Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);

Before we start the activity, we can use the putExtra() method to add information into the intent. Like so:

Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("STUDENT_NAME", "Bob");
startActivity(intent);

The putExtra method has two inputs. While typing this out, you might have noticed many different putExtra options show up in the autocomplete. The first input is always a string, and is the name. This is a unique identifier that acts as a key; you can use it later to retrieve the data. The second input can be many different types, as you can see in the documentation here: https://developer.android.com/reference/android/content/Intent.html\#pubmethods. The second input is the value, or data.

Now that we've put information into the intent in our first activity, we want to pull it out in the second activity. First, we get hold of the intent:

Intent intent = getIntent();

getIntent is a function built into every Activity that retrieves the intent that started this activity.

Bundle extras = intent.getExtras();

getExtras, as the name suggests, is sort of the opposite of putExtras. It pulls out the bundle of extras. A Bundle is a type that can hold different types of information in it.

String studentName = extras.getString("STUDENT_NAME");

We've used the getString method to retrieve our data using the key we defined earlier. While not mandatory, it is a common convention for the key to be capitalized snake case: https://en.wikipedia.org/wiki/Snake\_case. Other than getString, there are other methods to retrieve other types. Type "extras.get" in Android Studio to see the autocomplete suggest all the other options. You will need to pick the "get" function that matches the type of value you stores earlier using putExtra.

Now, there are a few things to be careful of. It is possible that the intent has no extras inside of it. In that case, getExtras() will return null. It is important to check for this.

Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
    String studentName = extras.getString("STUDENT_NAME");
}

We check if extras is null before proceeding. Otherwise, when you call getString on the extras object, we will be calling a method on a null object. This will cause a crash.

Since we wrote the app, we might be very sure that the intent will have extras in it. But it pays to be careful, because that may change in the future, and we won't remember to go to every usage and update it. It's also possible that there are multiple ways to get to an activity, and some of them involve extras while others don't.

A similar issue exists for the getString method. If there is no value for that particular key, or if the value for that key is NOT a string, it will return null. This is something we might want to check.

Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
    String studentName = extras.getString("STUDENT_NAME");
    if (studentName == null) {
        Log.d("MainActivity", "HELLO!");
    } else {
        Log.d("MainActivity", "HELLO " + studentName.toUpperCase() + "!");
    }
}

I've used the toUpperCase function as an example here to demonstrate why we need to be checking for null. If we call a method on a null object, we will get an exception and the app will crash.

Read: Have a look at the documentation for Bundle: https://developer.android.com/reference/android/os/Bundle.html\#inhmethods. No need to read everything, but have a look at methods like getInt() and getBoolean() to see what happens there when there is no matching value.

Try: Put two buttons on the first screen, "Count" and "Display count". When "Display count" is clicked, take user to another screen and tell them how many times "Count" was clicked. For example, if I clicked "Count" 8 times, when I click "Display count" it should take me to a screen that says "You clicked it 8 times!"

Hint: Break the problem into small steps: Keep track of the count and print to console. Create an intent to open new activity. Pass the count using the intent. Display the message to the user.

results matching ""

    No results matching ""