2

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?

10
  • developer.android.com/guide/components/… 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. Commented Nov 1, 2017 at 16:11
  • @MarcosVasconcelos but question about Activity not Service. And that link was already suggested ))) Commented Nov 1, 2017 at 16:52
  • The link points to Activities with intent filters Commented Nov 1, 2017 at 17:05
  • 1
    Cause you asked: new Intent("tj.xona.customintent.SecondActivity"); // Implicit intent this is a explicit intent with a String constructor Commented Nov 1, 2017 at 18:19
  • 1
    @jibrahim nice, now its the correct answer Commented Nov 1, 2017 at 18:52

1 Answer 1

2

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".

Sign up to request clarification or add additional context in comments.

10 Comments

And how would it look in form of implicit intent? How to "throw" it from the first activity and filter with <intent-filter>?
you can find detailed info here
and look here also
What if I need a custom action, not ACTION_SEND? How to define it and add to <intent-filter>?
You will find answer to that question here and there.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.