MobileAndroidHow to Start an Activity in a Different Android App

How to Start an Activity in a Different Android App

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Introduction

In a previous article, we had learned about intents. In this article, we will explore how we can start an activity in another application from within your current application.

Basics

To quickly recap, each Android activity is equivalent to a UI page that is shown in an app. To navigate to another page, we need to declare an intent.

Intents can be explicit or implicit.

Implicit intents are used when you want start an activity in another application simply by specifying the type of action you want to perform.

When we pass an implicit intent, the Android system resolves the intent to an app that can handle the intent and starts that app’s activity. If there is more than one app that can handle the intent, the user is asked to choose an app.

There are many implicit intents that are available for applications to declare and call that are provided by the Android system:

  • Alarms
    • Create an alarm
    • Create a timer
    • Show all alarms
  • Calendar
    • Add a calendar event
  • Camera
    • Capture a picture/video
    • Start camera app in still image mode
    • Start camera app in video mode
  • Contacts
    • Select a contact
    • Select specific contact data
    • View a contact
    • Edit an existing contact
    • Insert a contact
  • Email
    • Compose an email without optional attachments
  • File Storage
    • Retrieve a specific type of file
    • Open a specific type of file
  • Maps
    • Show a location on a map
  • Music and video
    • Play a media file
    • Play music based on search query
  • Phone
    • Initiate a phone call
  • Search
    • Search using a specific app
    • Perform a web search
  • Settings
    • Open a specific section of Settings
  • Text messaging
    • Compose a SMS/MMS message
  • Web browser
    • Load a Web URL

Hands On

We now will create a simple app that will use intents to perform a Web search.

Before we can use intents, we need to declare the intent filter. This will be in the AndroidManifest.xml file. We will discuss that in more detail later.

Fire up Android Studio and start a new Android Studio project.

Start1
Figure 1: Starting a new Android Studio project

Provide LaunchSearchDemo as the Application Name and click Next.

Start2
Figure 2: Configuring a new project

On the next screen, leave the default values and click Next.

Start3
Figure 3: Leaving the default values in place

On the “Add an activity to Mobile” page, choose “Blank Activity”. This creates an application with a single activity.

Start4
Figure 4: Adding a Blank Activity

We then are prompted to customize the activity. We will leave the default values unchanged.

Start5
Figure 5: Again, leaving the default values in place

Click Finish to create the project files.

Next, open AndroidManifest.xml and update it to declare our “SEARCH” intent.

lt;?xml version="1.0" encoding="utf-8"?>
<manifest xmlns_android="http://schemas.android.com/
   apk/res/android"
   package="com.example.vipul.launchsearchdemo" >

   <application
      android:allowBackup="true"
      icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      <activity
         android:name=".MainActivity"
         android:label="@string/app_name" >
         <intent-filter>
            <action android_name=
               "android.intent.action.MAIN" />

            <category android_name=
               "android.intent.category.LAUNCHER" />


            <action android_name=
               "com.google.android.gms.actions.SEARCH_ACTION"/>
            <category android_name=
               "android.intent.category.DEFAULT"/>


         </intent-filter>
      </activity>
   </application>
</manifest>

On the activity_main.xml page, add a button and an EditView control for the user to enter the search term. When the user clicks the Search button, the application will initiate a search.

<RelativeLayout xmlns_android="http://schemas.android.com/
      apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   tools:context=".MainActivity">

   <TextView android_text="@string/hello_world"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textView" />

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Search"
      android:id="@+id/buttonSearch"
      android:layout_alignBottom="@+id/editTextSearch"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true" />

   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editTextSearch"
      android:layout_below="@+id/textView"
      android:layout_marginTop="41dp"
      android:layout_alignRight="@+id/buttonSearch"
      android:layout_alignEnd="@+id/buttonSearch"
      android:layout_toRightOf="@+id/textView"
      android:layout_toEndOf="@+id/textView"
      android:text="Enter search term here" />

</RelativeLayout>

Next, we will add a couple of member variables and initialize then on the onCreate callback in the MainActivity.

public class MainActivity extends ActionBarActivity{

EditText editText;
Button buttonSearch;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   editText = (EditText)
      findViewById(R.id.editTextSearch);
   buttonSearch = (Button)
      findViewById(R.id.buttonSearch);
}

Lastly, we implement the click event handler of the button. In this method, we will get the search term from the editText and create an intent that declares its action as ACTION_SEARCH. We also will put the query term as an extra on the intent and fire off the intent.

public void onClick(View w) {
    String query = editText.getText().toString();
   Intent intent = new Intent(Intent.ACTION_SEARCH);
   intent.putExtra(SearchManager.QUERY, query);
   if (intent.resolveActivity(getPackageManager())
         != null) {
      startActivity(intent);
   }
}

One final thing we need to do is wire the onClick method to the onClick event for the button. Update activity_main.xml as shown here:

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Search"
   android:id="@+id/buttonSearch"
   android:layout_alignBottom="@+id/editTextSearch"
   android:layout_alignParentRight="true"
   android:layout_alignParentEnd="true"
   android:onClick="onClick"/>

Our application is now complete. When we start the application and provide a search term, we will be prompted to choose from one of the intent handlers and a search will be fired.

If you have trouble following along, you can download a sample of the code from the end of this article.

Summary

In this article, we learned about how to start an activity in another Android application. I hope you have found this information useful.

About the Author

Vipul Patel is a technology geek based in Seattle. He can be reached at vipul.patel@hotmail.com. You can visit his LinkedIn profile at https://www.linkedin.com/pub/vipul-patel/6/675/508.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories