Reputation: 349
I want to implement some activities using Implicit Intent. Currently, I have a button with "Display countries" label on this button. After I click this button, the child activity is triggered and the list of countries is displayed as ListView. When I choose some country I go back to the main activity where chosen country name is displayed in a TextView and toast widget. I was able to implement these actions using explicit activity, however, I do not know how to do it with an implicit one. In particular, I am not sure what parameters to set while creating new Intent instances.
Here is the Child Activity code:
public class ChildActivity extends AppCompatActivity {
ListView lvActionMode;
TextView textView;
Toast toast;
String selectedItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.items));
lvActionMode = findViewById(R.id.lvActionMode);
lvActionMode.setAdapter(adapter);
lvActionMode.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL ???);
lvActionMode.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
selectedItem = (String) adapterView.getItemAtPosition(position);
Intent replyIntent = new Intent(ChildActivity.this, MainActivity.class);
replyIntent.putExtra("selectedItem", selectedItem);
setResult(RESULT_OK, replyIntent);
startActivity(replyIntent);
}
});
}
}
And here is the main one:
public class MainActivity extends AppCompatActivity {
TextView textView;
Toast toast;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
toast = Toast.makeText(context, "popup", duration);
if (getIntent().hasExtra("selectedItem")) {
textView.setText(getIntent().getStringExtra("selectedItem"));
toast.setText(getIntent().getStringExtra("selectedItem"));
toast.show();
}
}
public void sendMessage(View view){
intent = new Intent(Intent.ACTION_VIEW ???? );
startActivityForResult(intent, RESULT_OK);
}
}
So how can I change an explicit Intent to an implicit one?
Upvotes: 3
Views: 815
Reputation: 392
In order for the Android OS to recognize your ChildActivity
as an activity to handle the data to be viewed, you'll need to declare an intent filter in the manifest. E.g:-
<activity android:name=".child_activity_name_here"
android:label="@string/app_name">
<intent-filter>
//Because you are calling Intent.ACTION_VIEW
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="text/plain"/> //type of your data you send in the Intent constructor
</intent-filter>
</activity>
Upvotes: 1
Reputation: 11995
If you check out the docs you'll see that if you want your activity to respond to implicit intents you need o register an intent filter in your manifest file, like down bellow. Mind you if you do that, you need to create a category specific to your needs so the intent won't open another application.
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
Remember how implicit intents work, android will look in the system for an activity that matches the action, category and data type you set when you create the intent, so make sure your intent is unique to your activity (by creating a custom category for example).
It's pretty simple to create a category:
static final String CATEGORY_COUNTRY = "com.example.category.COUNTRY";
To avoid headaches always check if your implicit intents will resolve to an actual activity so your app won't crash:
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
Upvotes: 1