- send the data through intent using putExtra() method.
- We can set all primitive data types and non-primitive data types in the intent.
Intent intent = new Intent(MainActivity.this, SecActivity.class);
intent.putExtra(“key”, ”value”);
intent.putExtra(“int_key”, 45);
startActivity(intent);
- to get the data in second activity we need the same intent object which is used to start the second activity.
- To get the intent object we need to call getIntent() which is belongs to Activity class.
- to get the data getStringExtra() method should be called.
- Key should be passed as a parameter to get the value for this method.
Intent intent = getIntent();
String value = getStringExtra(“key”);
This method return the value of an item that previously added with putExtra() or null if no String value was found.Getting Data From Called Activity:
- startActivityForResult() method should be used instead of startActivity() method.
- For this method we need to pass two parameter, Intent object and requstcode as integer.
- Parameters:
requestCode If >= 0, this code will be returned in onActivityResult(),when the activity
exits.
Throws:
android.content.ActivityNotFoundException
Example:
Intent intent = new Intent(MainActivity.this, SecActivity.class);
startActivityForResult( intent, 1);
- In second Activity setResult() method should be called to set the result for the first activity.
- Intent object and resultCode as integer should be passed as a parameters.
- Parameters
activity, often RESULT_CANCELED or RESULT_OK
data The data to propagate back to the originating activity.
Example:
Intent intent = new Intent();
intent.putExtra(“name”,”kumar”);
setResult(RESULT_OK, intent);
- To get the data back in the first activity onActivityResult() method should be override.
- You will receive this call immediately before onResume() when your activity is re-starting.
- This method will have three parameters
protected void onActivityResult(int requestCode,int resultCode,Intent data)
- This method is never invoked if your activity sets noHistory to true in AndroidManifest.xml for the activity tag.
- Parameters:
startActivityForResult(), allowing you to identify who this
result came from.
resultCode The integer result code returned by the child activity
through its setResult().
data An Intent, which can return result data to the caller
(various data can be attached to Intent "extras").
Example:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 2) {
if (resultCode == RESULT_OK) {
String name = data.getStringExtra( “name” );
}
}
}
Starting other Apps Activity
- There are two primary forms of intents you will use.
- Explicit Intents have specified a component , which provides the exact class to be run. Often these will not include any other information, simply being a way for an application to launch various internal activities it has as the user interacts with the application.
- Implicit Intents have not specified a component; instead, they must include enough information for the system to determine which of the available components is best to run for that intent. Implicit intents do not declare the class name of the component to start, but instead declare an action to perform.
Intent intent = new Intent( Intent.ACTION_SEND);
intent.setType(“text/plain”);
startActivity(intent);
Allowing Other Apps to Start Your Activity
- To allow other apps to start your activity, you need to add an <intent-filter> element in your manifest file for the corresponding <activity> element.
- When your app is installed on a device, the system identifies your intent filters and adds the information to an internal catalog of intents supported by all installed apps.
- When an app calls startActivity() or startActivityForResult() , with an implicit intent, the system finds which activity (or activities) can respond to the intent.
- The system may send a given Intent to an activity if that activity has an intent filter fulfills the following criteria of the Intent object.
Data <data> -- A description of the data associated with the intent.
Example :
<activity android:name="ShareActivity">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
0 comments:
Post a Comment