intent-filter. Now, let's start!
Starting 2 new projects
- The first project has an
ActivitynamedFirstActivityand it's layout file is activity_first.xml - The second project has an
ActivitynamedSecondsActivityand it's layout file is activity_seconds.xml
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
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
Intentaction name that will be used to call the activityinfo.devexchanges.secondsapp.SECOND_ACTIVITYin the project 2 AndroidManifest.xml. Put this code in<activity>scope ofSecondsActivity:
<intent-filter>
<action android:name="info.devexchanges.secondsapp.SECOND_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Launching SecondsActivity from FirstActivity
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
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
And this is output:
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();
}
});
}
}
Conclusions
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.