Kotlin
Java

MP2: Launching An Activity
Kotlin

Created By: Geoffrey Challen
/ Updated: 2022-11-06

Let's continue our work on the machine project! In this lesson we'll first wrap up some leftover networking work in our API client. Then we'll shift our attention to the remaining steps needed to finish this feature: support for adding new favorite places in our app's user interface.

API Client

At this point we've added support to our server for receiving POST requests from clients. But we still haven't added that capability to the API client used by our app. So let's wrap this up before we shift our focus to our new activity.

When you're done, you should be able to pass test1_ClientFavoritePlacePost. Note that this method is quite similar to code that we've provided, and most of the hard work is already done in the server. So we're not going to provide too much detailed guidance here.

Return to the API client and discuss how to complete the postFavoritePlace method. Discuss what to do and how to override methods on the StringRequest object, but don't provide too much code or guidance. Let them figure this one out a bit on their own.

Launching the Add Place Activity

Now that we have support for adding new favorite places both in our backend server and in our API client, we need to address one of the more interesting parts of this task: interaction with the user!

At present our app allows users to browse the map, view favorite places, and click on them to see more details. What do we need to allow them to add their favorite place?

  1. We need a way for them to initiate this process, via some interaction with the map
  2. We need to know where their favorite place is—it's latitude and longitude
  3. We also need to know all of the other details about their favorite place, the same information we are currently loading about other favorite places from places.csv: a unique ID, a name, and a description.

There are multiple ways that you could initiate this dialog and collect the required information. Here's how we're going to approach it:

  • We'll set up the map so that a long press will initiate the process of adding a place to the map (#1) and also mark the location of the favorite place (#2)
  • We'll then launch a new activity (screen) in the app that will collect the description and allow them to submit their new favorite place (#3). (We'll discuss later how to acquire the ID and name.)

Today we'll work on adding support for long presses to our current map view. We'll also discuss how to launch a new activity in Android, and how to pass that activity data that it might need—in our case, the latitude and longitude. That will allow us to pass test2_LongPressLaunchesActivity.

Adding a Long Press Listener

First, let's discuss how to add the ability to handle long press events on our existing map. This is somewhat non-obvious and requires understanding of osmdroid, the mapping library we're using. So we'll step through it together in the video below.

Go through the process of adding a layer to the map that can handle long press events. Show how that event contains the location that triggered it.

Launching a New Activity

To continue, we need to discuss the process of launching a new activity in Android—meaning moving from one screen in the app to another. We do this by using an Intent, which we'll demonstrate in the walkthrough below. We also need to make some changes to our Android manifest before this will work properly.

Discuss how to launch a new activity. Adjust the manifest properly, and hint at using extras.

AddPlaceActivity Launch Specification

At this point we're nearly done with our third MP2 test case! For that test case to pass, a long press on the map should launch the AddPlaceActivity. That activity can be empty, but the Intent used to launch it must contain the latitude and longitude location of the long press on the map, as extras with the keys latitude and longitude respectively. Note that you should add these values as Strings, not as Doubles, since this makes it easier to detect missing values. (Because String?s can be null.)

No Homework for This Lesson

As a reminder, on lessons where we focus on the machine project we will not assign a homework problem! However, the lesson will usually focus on helping you complete a particular part of the MP test suite, and so we encourage you to spend time on that in lieu of a homework problem.

Right now your goal should be to

  1. Finish the postFavoritePlace method on your API client and pass test1_ClientFavoritePlacePost.
  2. Complete the work necessary to pass test2_LongPressLaunchesActivity, specifically adding information to the Intent correctly.

If you get stuck, find us for help on the help site or forum.

MP2 Scores

More Practice

Need more practice? Head over to the practice page.