开发者

Is Intent.ACTION_SEND a valid intent protocol for startActivityForResult()?

开发者 https://www.devze.com 2023-04-03 04:53 出处:网络
I want the users of my app to be able to send an email and then return to my app. I\'ve implemented this featur开发者_开发知识库e like below

I want the users of my app to be able to send an email and then return to my app.

I've implemented this featur开发者_开发知识库e like below

Intent emailIntent = new Intent(Intent.ACTION_SEND);         
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL  , new String[]{"test@gmail.com"}); 
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Message subject"); 
emailIntent.putExtra(Intent.EXTRA_TEXT   , "Message text"); 
try { 
    startActivityForResult(Intent.createChooser(emailIntent, "Send mail..."), 42);

} catch (android.content.ActivityNotFoundException ex) { 
    Toast toast = Toast.makeText(getApplicationContext(), "There are no email clients installed.", Toast.LENGTH_SHORT);
    toast.show();
} 

I have tested the implementation on a HTC Sensation, and it works fine, at least when selecting the Gmail app.

My problem is this section in the documentation for startActivityForResult() "Note that this method should only be used with Intent protocols that are defined to return a result. In other protocols (such as ACTION_MAIN or ACTION_VIEW), you may not get the result when you expect. For example, if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result."

I have searched for a list of intent protocols that is intended to be used from startActivityForResult() but havent found any.

Is there such a list?

Is it OK to use Intent.ACTION_SEND from startActivityForResult()? Can I expext the same behavior on all devices?

Thanks

/Mathias


It is OK (you will not get any errors) to do that. However, you will not receive any relevant result in onActivityResult, which is why you should just call startActivity().

The platform does not enforce any rules over which intent actions should or should not be used with startActivityForResult. The docs are just advising that you should only use this method with intents that are documented to return something.

If you are relying on onActivityResult() to determine when the other activity has finished and your activity is resumed, that is incorrect. Just use onResume() instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消