What Is The Different Between Explicit and Implicit Activity Call in Android

What is the different between Explicit and implicit activity call in android?

For example:

implicit activity call

In intent filter you create action for you activity, so other app can call your activity via this action as following:

<activity android:name=".BrowserActivitiy" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http"/>
</intent-filter>
</activity>

And the other way to call implicit Intent is below:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
startActivity(intent);

Explicit activity call

You make a call that indicate exactly which activity class:

Intent intent = new Intent(this, ActivityABC.class);
intent.putExtra("Value", "This value for ActivityABC");
startActivity(intent);

Hope this help you understand more about Explicit and implicit activity call in android.

You can get more detail about Android Intent here

Difference between implicit and explicit intents

  1. Explicit Intent: Explicit intent names the component.

  2. Implicit Intent: Implicit Intents have not specified a component.

E.g: The java class which should be called Implicit intent asked the system to perform a service without telling the system which java class should do this service.

Android implicit intents VS explicit intents

Implicit Intents do not directly specify the Android components which should be called, it only specifies action to be performed. A Uri can be used with the implicit intent to specify the data type.

for example

Intent intent = new Intent(ACTION_VIEW,Uri.parse("http://www.google.com"));

this will cause web browser to open a webpage. Android system searches for all components which are registered for the specific action and the data type.If many components are found then the user can select which component to use..

Explicit intents are used in the application itself wherein one activity can switch to other activity...Example Intent intent = new Intent(this,Target.class); this causes switching of activity from current context to the target activity.
Explicit Intents can also be used to pass data to other activity using putExtra method and retrieved by target activity by getIntent().getExtras() methods.

Hope this helped.

Android Implicit and Explicit Intents

Implicit intents is when you want to perform an action but you don't know what application the user currently has to handle that action.
For example sending an email, there are many applications for that so the user can choose which application he wants to use.

Explicit intent is used to start an activity within your application, if you have mainactivity, and secondActivity, and you want to start the second activity you call an explicit intent.

StartActivity(new Intent(getBaseContext(), secondActivity.class));

You can pass data between activities by adding extras to the bundle that is being passed with the Intent.

Intent i = new Intent(getBaseContext, secondActivity.class);
i.putExtra("key",value);
startActivity(i);

And to get back the extras in your second activity just call:

getIntent().getStringExtra("key");

Or if you want only to get the "data" uri that was passed you can call

getIntent().getData();

The extra can be for example an int/double/String or a parcable object

http://developer.android.com/reference/android/os/Parcelable.html

Explicit intents, implicit intents, and broadcasts

From the android documentation:

Explicit intents specify the component to start by name (the
fully-qualified class name). You'll typically use an explicit intent
to start a component in your own app, because you know the class name
of the activity or service you want to start. For example, start a new
activity in response to a user action or start a service to download a
file in the background.

Implicit intents do not name a specific component, but instead declare
a general action to perform, which allows a component from another app
to handle it. For example, if you want to show the user a location on
a map, you can use an implicit intent to request that another capable
app show a specified location on a map.

Explicit intents, as you've said, are used to start an activity in your application -- or to transition from one "screen" to another. An explicit intent would be something like Intent intent = new Intent(currentContext, ActivityB.class); These types of intents are used when you are within your application and explicitly know which component you want to start depending on how the user interacts with your activity.

Implicit Intents don't directly specify the Android components which should be called, but rather just specify a general action to be performed. These are generally used when you would like some external application to do something for you. An example of an implicit intent used to send an email using an external application would be:

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"someemail@gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject");
i.putExtra(Intent.EXTRA_TEXT , "body");

This intent would query for applications installed on the device that can handle sending an email, but there could be quite a few applications that can do this -- for example, if we have a gmail application, hotmail application, etc. So, basically you're just specifying a general action and asking the system "who can handle this", and the system will handle the rest. These types of intents are used by application developers so they don't have to "reinvent the wheel" if there is already something on the device that can do what the developer wants.

Here's a couple of references that might help explain it better:

http://developer.android.com/guide/components/intents-filters.html

http://www.vogella.com/articles/AndroidIntent/article.html

Explicit and Implicit intents

The linked example includes these lines

      Intent explicit=new Intent(implicit);
ComponentName cn=new ComponentName(svcInfo.applicationInfo.packageName,
svcInfo.name);

explicit.setComponent(cn);

The first line simply creates a new Intent instance that's a copy of the old one. While the variable might be explicit, at this point in time, it is still an implicit intent.

The second line creates a ComponentName object based on the single matching result from an earlier getActivity().getPackageManager().queryIntentServices(implicit, 0) call. At this point, explicit is still an implicit intent.

The third line is the magic. Once the specific ComponentName is assigned to the Intent instance, explicit truly becomes an explicit intent.

Explicit and implicit form of the same intent

Define an integer constant, for example:

private static final int REQUEST_CODE = 1;

Create a new Intent in your Activity class:

Intent intent = new Intent(this, DestinationActivity.class);
startActivityForResult(intent, REQUEST_CODE);

In this Activity class override following method:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
// do something
}
}

In your destination Activity class, DestinationActivity, you have to create a new Intent to hold a data:

Intent data = new Intent();
data.putExtra("boolean1", true);
data.putExtra("boolean2", false);

To pass data back to the source Activity you have to call the following method:

setResult(REQUEST_CODE, data); // will call onActivityResult() method

For more info look here and there

If you would like to send a text through other app in your phone you can use an explicit intent or ShareCompat class (which is provided by v4 Support Library). Example with ShareCompat:

Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setSubject("ShareCompat")
.setText("I am using ShareCompat class")
.setChooserTitle("Sending Text")
.createChooserIntent();

if (shareIntent.resolveActivity(getPackageManager()) != null)
startActivity(shareIntent);

Example of explicit and implicit intents:

1) manifest file:

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="tj.xona.customaction" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

2) MainActivity class:

public class MainActivity extends AppCompatActivity {

private TextView textView;
private Button button;

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

textView = (TextView) findViewById(R.id.outputText);
button = (Button) findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent intent = new Intent(MainActivity.this, SecondActivity.class); // Explicit intent
Intent intent = new Intent(); // Implicit intent
intent.setAction("tj.xona.customaction"); // custom action
startActivityForResult(intent, SecondActivity.CUSTOM_INTENT);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SecondActivity.CUSTOM_INTENT && resultCode == RESULT_OK) {
String msg = data.getStringExtra(SecondActivity.MESSAGE_BACK);
textView.setText(msg);
}
}
}

3) SecondActivity class:

public class SecondActivity extends AppCompatActivity {

public static final int CUSTOM_INTENT = 1;
public static final String MESSAGE_BACK = "message";

private EditText edit;
private Button send;

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

edit = (EditText) findViewById(R.id.edit);
send = (Button) findViewById(R.id.send);

send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = edit.getText().toString();

Intent intent = new Intent();
intent.putExtra(MESSAGE_BACK, msg);

setResult(RESULT_OK, intent);
finish();
}
});
}
}

Conclusion: You can use explicit and implicit intents for the Activity which is defined with intent-filter in your app. But if you want to use an activity from another app you must use implicit intent. Inside your app it's better to use explicit intent to start an activity. The idea of using implicit intent is reusing some activity from another apps in your phone. When you follow standard action names that will make easy to use some functionality and most interesting you can have multiple choices. By using custom action for your activity you restrict your app because nobody knows about this custom action, such as in this example: "tj.xona.customaction".

starting android service using explicit vs implicit intent

A1 : Even though your activity and service run in different processes they still belong to same Application. You can still use explicit intent, I don't see any specific advantage of using implicit intent here (let me know if find any :) )

A2 : let me list down few facts here

  • Life cycle of "Started" service (rather than "Bind"ed service) is independent of the life cycle of Activity which has started this service. This is true irrespective whether both are running in the same process or not.
  • Only one instance of Service will be alive at any point of time. when your activity calls startService() , service instance will be created if it is not already running (in this case you service will receive onCreate() callback as well). But if Service is already running, Framework would simply call onStartCommand() callback on already running process(No onCreate() callback in this case). Again all this is true irrespective of activity and service are running on same process or different processes.

Now to answer your question, if you service is still running (because of startService() call by previous activity), then bindService()/startService() will make sure to connect to existing service.

Hope this is of some help to you. Let me know if you have any other specific questions.



Related Topics



Leave a reply



Submit