Tuesday 12 February 2013

Explicity Intent and its example



--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Explicity Intent

In an explicit intent, we actually specify the activity that is required to respond to the intent. In other words, we explicitly designate the target component. This is typically used for application internal messages. The code of this post can be found at end.
In an implicit intent, the main power of the android design, we just declare an intent and leave it to the platform to find an activity that can respond to the intent. Here, we do not declare the target component and hence is typically used for activating components of other applications seamlessly
Let’s look at our example:
This example has 2 activities:
  • InvokingActivity
  • InvokedActivity
The InvokingActivity has a button “Invoke Next Activity” which when clicked explicitly calls the InvokedActivityclass. The relevant part of the code is here:
Button invokingButton = (Button)findViewById(R.id.invokebutton);
invokingButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent explicitIntent = new Intent(InvokingActivity.this,InvokedActivity.class);
startActivity(explicitIntent);
}
});
The layout for InvokingActivity is defined in /res/main.xml:
and for InvokedActivity in /res/invokedactivity.xml.
Here are our java code, InvokingActivity.java:
package com.bogotobogo.explicitintent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class InvokingActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button invokingButton = (Button)findViewById(R.id.invokebutton);
invokingButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent explicitIntent = new Intent(InvokingActivity.this,InvokedActivity.class);
startActivity(explicitIntent);
}
});
}
}
and InvokedActivity.java:
package com.bogotobogo.explicitintent;
import android.app.Activity;
import android.os.Bundle;
public class InvokedActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.invokedactivity);
}
}

For Implicity Intent follow this link
Implicit Intent

No comments:

Post a Comment