Communication Between Activities
×
Home Courses Community Support
Gray Matter

First Step Towards Android: Introduction »

Understanding Activities and Intents »

Hands On With UI Designing In Android (Controls-Views,Widgets and Layouts) »

Updating... »

Communication Between Activities

Resolution of intent filter collision

In the previous section, you learned that the <intent-filter> element defines how your activity can be invoked by another activity. What happens if another activity (in either the same or a separateapplication) has the same filter name?

For example, suppose your application has another activity named Activity3, with the following entry in the AndroidManifest.xml file:
 

<?xml version=”1.0” encoding=”utf-8”?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”net.learn2develop.Activities”
android:versionCode=”1”
android:versionName=”1.0”>
<application android:icon=”@drawable/icon” android:label=”@string/app_name”>
<activity android:name=”.MainActivity”
android:label=”@string/app_name” >
<! -- android:theme=”@android:style/Theme.Dialog” -- >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.Activity2”
android:label=”Activity 2”>
<intent-filter>
<action android:name=”net.learn2develop.ACTIVITY2” />
<category android:name=”android.intent.category.DEFAULT” />
</intent-filter>
</activity>
<activity android:name=”.Activity3”
android:label=”Activity 3”>
<intent-filter>
<action android:name=”net.learn2develop.ACTIVITY2” />
<category android:name=”android.intent.category.DEFAULT” />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion=”9” />
</manifest>

 

If you call the startActivity() method with the following intent, then the Android OS will display a selection as shown below-

startActivity(new Intent(“net.learn2develop.ACTIVITY2”));

If you check the “Use by default for this action” item and then select an activity, then the next timethe intent “net.learn2develop.ACTIVITY2” is called again, it will always launch the previous activitythat you have selected.

To clear away this default, go to the Settings application in Android and select Applications ?Manage applications and select the application name (see Figure 2-15). When the details of theapplication are shown, scroll down to the bottom and click the Clear defaults button.

 

Output -


 


 

Return results from intents

The startActivity() method invokes another activity but does not return a result to the currentactivity. For example, you may have an activity that prompts the user for username and password.
The information entered by the user in that activity needs to be passed back to the calling activityfor further processing. If you need to pass data back from an activity, you should instead use the startActivityForResult() method.

  • Using the same project created in the previous section, add the following statements in bold to the main.xml file:
<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Please enter your name” />
<EditText
android:id=”@+id/txt_username”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” />
<Button
android:id=”@+id/btn_OK”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”OK” />
</LinearLayout>

 

  • Add the following statements in bold to Activity2.java:
package net.learn2develop.Activities;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Activity2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
//---get the OK button---
Button btn = (Button) findViewById(R.id.btn_OK);
//---event handler for the OK button---
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
Intent data = new Intent();
//---get the EditText view---
EditText txt_username =
(EditText) findViewById(R.id.txt_username);
//---set the data to pass back---
data.setData(Uri.parse(
txt_username.getText().toString()));
setResult(RESULT_OK, data);
//---closes the activity---
finish();
}
});
}
}

 

  • Add the following statements in bold to the MainActivity.java file:
package net.learn2develop.Activities;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.KeyEvent;
import android.widget.Toast;
import android.content.Intent;
public class MainActivity extends Activity {
String tag = “Events”;
int request_Code = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//---hides the title bar---
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Log.d(tag, “In the onCreate() event”);
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
{
//startActivity(new Intent(“net.learn2develop.ACTIVITY2”));
//startActivity(new Intent(this, Activity2.class));
startActivityForResult(new Intent(
“net.learn2develop.ACTIVITY2”),
request_Code);
}
return false;
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == request_Code) {
if (resultCode == RESULT_OK) {
Toast.makeText(this,data.getData().toString(),
Toast.LENGTH_SHORT).show();
}
}
}
public void onStart() { //... }
public void onRestart() { //... }
public void onResume() { //... }
public void onPause() { //... }
public void onStop() { //... }


Output -


 

Passing Data Using Intent Objects

Besides returning data from an activity, it is also common to pass data to an activity. For example, in the previous example you may want to set some default text in the EditText view before the activity is displayed. In this case, you can use the Intent object to pass the data to the target activity.

  • Using the same project created in the previous section, add the following statements in bold to the MainActivity.java file.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
{
//startActivity(new Intent(“net.learn2develop.ACTIVITY2”));
//startActivity(new Intent(this, Activity2.class));
/*
startActivityForResult(new Intent(
“net.learn2develop.ACTIVITY2”),
request_Code);
*/
Intent i = new Intent(“net.learn2develop.ACTIVITY2”);
Bundle extras = new Bundle();
extras.putString(“Name”, “Your name here”);
i.putExtras(extras);
startActivityForResult(i, 1);
}
return false;
}

 

  • Add the following statements in bold to Activity2.java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
String defaultName=””;
Bundle extras = getIntent().getExtras();
if (extras!=null)
{
defaultName = extras.getString(“Name”);
}
//---get the EditText view---
EditText txt_username =
(EditText) findViewById(R.id.txt_username);
txt_username.setHint(defaultName);
//---get the OK button---
Button btn = (Button) findViewById(R.id.btn_OK);
//---event handler for the OK button---
btn.setOnClickListener(new View.OnClickListener()
{
//...
});
}

 

Output -

 

 

← Preview

Intent and Its Type

Next →

Calling Built in Apps

NOTES

Our mission is to provide highly effective and quality education via innovative solutions. Geeksdemy look forward to bridge the gap between in-demand technology and academics in order to deliver innovative, easy, interesting and affordable learning across the Globe.

Courses

  • Python
  • Game Development with Unity 3D
  • Arduino
  • PCB & Circuit Designing
  • Robotics and Embedded C
  • 8051/8052 Embedded Systems
  • QuadCopter & UAV
  • MATLAB with Robotics
  • Raspberry Pi
Learn On the Go!
Get the Android App
Get the iOS App