Android - Launch another Application's activity and get it's result

    In Android development, sometimes you must run another application which installed in the device to performing a reference work. You have become accustomed to launch a system app like Camera or Contact to get it's result (photo, contact info) but in this post, I would like to present the way to invoke another "normal" app (which can be developed by another developer) by using intent-filter. Now, let's start!

Starting 2 new projects

Open Android Studio and start 2 new project:
  • The first project has an Activity named FirstActivity and it's layout file is activity_first.xml
  • The second project has an Activity named SecondsActivity and it's layout file is activity_seconds.xml
Put some XML code in 2 layouts file to build the interface:
activity_first.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="info.devexchanges.firstapplication.FistActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This is the first Activity" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:text="Go to another app activity" />
</RelativeLayout>
activity_seconds.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_second"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="info.devexchanges.secondapplication.SecondsActivity">

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Put some texts" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/edit_text"
        android:text="Back to the first Activity" />
</RelativeLayout>

Configurations in AndroidManifest.xml

    Of course, we'll launch SecondsActivity of application 2 from FirstActivity later. To perform this work, we need use intent-filter to define the specifies the type of Intent accepted based on the Intent's name, data, and category. Now, do this work:
  • Keep the default “generated” AndroidManifest.xml without changes in the project one.
  • Here is an important step, we need to define the Intent action name that will be used to call the activity info.devexchanges.secondsapp.SECOND_ACTIVITY in the project 2 AndroidManifest.xml. Put this code in <activity> scope of SecondsActivity:
<intent-filter>
       <action android:name="info.devexchanges.secondsapp.SECOND_ACTIVITY" />
       <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Launching SecondsActivity from FirstActivity

    With the configuration above, we now can launch SecondActivity easily from FirstActivity with this code:
         try {
                    Intent intent = new Intent("info.devexchanges.secondsapp.SECOND_ACTIVITY");
                    startActivity(intent);
                } catch (ActivityNotFoundException ex) {
                    ex.printStackTrace();
                    Log.e("Main", "Second application is not installed!");
                }
NOTE: You can put data to SecondsActivity by use putExtras() method of Intent (before call startActivity()).
    We'll have this output:

Get result from SecondActivity

    In order to get the result from SecondsActivity, we must use startActivityForResult() method instead of startActivity() in the FirstActivity:
         try {
                    Intent intent = new Intent("info.devexchanges.secondsapp.SECOND_ACTIVITY");
                    startActivityForResult(intent, REQUEST_CODE);
                } catch (ActivityNotFoundException ex) {
                    ex.printStackTrace();
                    Log.e("Main", "Second application is not installed!");
                }
Moreover, you must override onActivityResult() to get Intent data and resultCode which returned from SecondsActivity:
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                String intentData = data.getStringExtra("EditText_Value");
                textView.setText(intentData);
            } else {
                textView.setText("User press back at Second Activity");
            }
        }
    }
In SecondsActivity, before it's finish, just call setResult() to send back resultCode to the parent Activity:
SecondsActivity.java
package info.devexchanges.secondapplication;

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

public class SecondsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seconds);

        View btnBack = findViewById(R.id.button);
        final EditText editText = (EditText) findViewById(R.id.edit_text);

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("EditText_Value", editText.getText().toString().trim());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
}
And this is output:

Conclusions

    With defining an intent-filter option, we can launch another app activity easily and get it's result. Hope this post is helpful in developing "reference applications" in your work. Moreover, please go to the Google official document to read more about Intent + Intent Filter, one of basic concept of Android development.

Share


Previous post
« Prev Post
Next post
Next Post »