How to create a dialog created by the function Intent.createChooser () cancelable?

How to create a dialog created by Intent.createChooser() cancelable? In other words: when I go beyond this dialog box, I need to cancel it.

By default, it works as follows. But on some devices (for example, Samsung GT2) this is not so (this dialog can be canceled only by pressing the back button).

NOTE I am talking about a dialog created by Intent.createChooser() . I do not have a link to Dialog .

+4
source share
2 answers

I agree with Yul - this is impossible, except to create your own choice. queryIntentActivities() can provide you with the contents of a list, and you can create your dialog as you wish. Here is an example project using queryIntentActivities() to populate a ListView all LAUNCHER activities, and then using this data to trigger a click-on entry.

Personally, since most devices will have this behavior already, I would not worry. I use Android from the original devices, and I did not understand that this dialog box was canceled by other means, except the Cancel button (on older devices) and the BACK button until you read your question. I am skeptical that your user base will prevent your application from working just like all other applications on your device in terms of selection behavior.

+2
source

If you do this with startActivityForResult , you can get the resultCode and check if it has RESULT_CANCELED .

More information about this is in the training documentation for obtaining the result from Activity and documentation for Activity .

While shamelessly borrowing examples from the above documentation, I will show you how I do it:

 static final int PICK_CONTACT_REQUEST = 1; private void pickContact() { Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts")); pickContactIntent.setType(Phone.CONTENT_TYPE); startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { ... } else if (resultCode == RESULT_CANCELED){ ... } } 
+1
source

Source: https://habr.com/ru/post/1443417/


All Articles