Lesson Weekend

Now that we have a functioning clickListener for our findRestaurantsButtons, let’s work towards making it navigate to a new Activity where we will display information about the restaurants.

Creating New Activities

Right click on the package name and select New > Activity > Empty Activity. Let’s name this Activity, RestaurantsActivity, and make sure that the Generate Layout File box is checked, so that a corresponding .xml layout file for this activity is automatically created.

Navigating Between Activities

With our RestaurantsActivity created, let’s change the code inside of the onClick() method for our mFindRestaurantsButton so that it navigates to this new activity when clicked, instead of displaying a Toast.

We can accomplish this by using an intent. An Intent represents something our app “intends to do” by describing a simple action you'd like the app to perform. Most often, an intent is used to start another activity or to hand something over to another app.

Erase the toast in the onClick() method and replace it with the two lines of code detailed below:

MainActivity.java
package com.epicodus.myrestaurants;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button mFindRestaurantsButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mFindRestaurantsButton = (Button) findViewById(R.id.findRestaurantsButton);
        mFindRestaurantsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, RestaurantsActivity.class);
                startActivity(intent);
            }
        });
    }
}

Here, we are constructing a new instance of the Intent class with the line Intent intent = new Intent(MainActivity.this, RestaurantsActivity.class);. As you can see this takes two parameters: The current context, and the Activity class we want to start.

The current context is the environment in which our code is running. Then, we are calling the startActivity() method which takes an intent as a parameter. So, when we click the mFindRestaurantsButton a new Intent that takes the user from the MainActivity to the RestaurantsActivity will be created and immediately started.

Let’s run our app again. We should now be able to navigate to our new activity by clicking on our findRestaurantsButton!


Example GitHub Repo for MyRestaurants

Terminology


  • Intent: An instance of the Intent class; represents something our app "intends to do"; most often, an intent is used to start another activity or hand something over to another app.

Tips


  • Create a new activity by right-clicking on the package, then selecting New > Activity > Empty Activity.

Examples


mFindRestaurantsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, RestaurantsActivity.class);
                startActivity(intent);
            }
        });