How would the same custom intent look in forms of explicit and implicit one? By "custom" I mean it's not ACTION_VIEW
or something like that. It is intent used to open one activity (for example called activB
) from another (activA
) in the same app and return some results (a couple of boolean
vars) to the first activity (activA
). How would one able to implement that?

- 407
- 5
- 21
-
https://developer.android.com/guide/components/intents-filters.html?#Receiving Implicit intents are shared with the system to receive the ACTIONS, so you may shown up a choose dialog from your app by declaring the same filter of another installed application. From doc: Caution: To avoid inadvertently running a different app's Service, always use an explicit intent to start your own service and do not declare intent filters for your service. – Marcos Vasconcelos Nov 01 '17 at 16:11
-
@MarcosVasconcelos but question about Activity not Service. And that link was already suggested ))) – Ibrokhim Kholmatov Nov 01 '17 at 16:52
-
The link points to Activities with intent filters – Marcos Vasconcelos Nov 01 '17 at 17:05
-
@MarcosVasconcelos in my first comment – Ibrokhim Kholmatov Nov 01 '17 at 17:24
-
Oh.. I tought you was the owner of the question, the link is for Activities anyway – Marcos Vasconcelos Nov 01 '17 at 17:48
-
@MarcosVasconcelos answer is mine, I guess you look at the answer's links but I am saying in first comment of the answer which I wrote two days ago. By anyway this is not an issue. – Ibrokhim Kholmatov Nov 01 '17 at 18:10
-
@MarcosVasconcelos My answer was down voted for some reason, do you think that something wrong with it? – Ibrokhim Kholmatov Nov 01 '17 at 18:13
-
1Cause you asked: new Intent("tj.xona.customintent.SecondActivity"); // Implicit intent this is a explicit intent with a String constructor – Marcos Vasconcelos Nov 01 '17 at 18:19
-
@MarcosVasconcelos ohh sorry, my fault(( fixed it, thank you for feedback! – Ibrokhim Kholmatov Nov 01 '17 at 18:34
-
1@jibrahim nice, now its the correct answer – Marcos Vasconcelos Nov 01 '17 at 18:52
1 Answers
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".

- 1,079
- 2
- 13
- 16
-
And how would it look in form of implicit intent? How to "throw" it from the first activity and filter with `
`? – VELFR Oct 30 '17 at 06:12 -
you can find detailed info [here](https://developer.android.com/guide/components/intents-filters.html#ExampleSend) – Ibrokhim Kholmatov Oct 30 '17 at 06:41
-
and look [here](https://medium.com/google-developers/sharing-content-between-android-apps-2e6db9d1368b) also – Ibrokhim Kholmatov Oct 30 '17 at 07:29
-
What if I need a custom action, not `ACTION_SEND`? How to define it and add to `
`? – VELFR Oct 30 '17 at 10:28 -
You will find answer to that question [here](https://stackoverflow.com/questions/10921451/start-activity-using-custom-action) and [there](https://stackoverflow.com/questions/11490810/how-to-start-an-activity-using-a-custom-intent). – Ibrokhim Kholmatov Oct 30 '17 at 10:56
-
Is it possible to not mention `SecondActivity` in code of `MainActivity` and get the same results? – VELFR Nov 02 '17 at 19:57
-
-
No. I mean, is it possible to implement `onActivityResult` without `SecondActivity`? Is there anything like `ResultActivity = getActivityResultByResultCode(resultCode)` to make `data.getStringExtra(ResultActivity.MESSAGE_BACK);` or something like that? Or I can access result's data without mentioning `SecondActivity`? – VELFR Nov 05 '17 at 12:11
-
of course, you can. That was just example. For instance you can do it like this: startActivityForResult(intent, 1); and if (requestCode == 1 && resultCode == RESULT_OK). But it's better to declare that values as constant somewhere in your code and not necessary in SecondActivity – Ibrokhim Kholmatov Nov 05 '17 at 15:12
-
the same for: String msg = data.getStringExtra("anything_you_want"); and intent.putExtra("anything_you_want", msg); – Ibrokhim Kholmatov Nov 05 '17 at 15:17