Android: Sharing (something) via Intent.ACTION_SEN

2019-05-08 19:20发布

问题:

I would like to share something from my application. Once it is shared (e.g - message sent), I want my application to be active again and the sending app to disappear. Using the code below, I expected onActivityResult to be called, but it is never called.

After sending email, my application appears again, but after sending SMS ('Messaging') the messaging app remains. (onActivityResult is never called)

Thanks :-)

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain"); 
String shareBody = "This is a test";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "You have to see this!");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);

startActivityForResult(Intent.createChooser(sharingIntent, "Share via"),1);

getFragmentManager().popBackStack();

回答1:

This will only be called if the user presses the back button. Because you are starting a new intent, so you give control to another application. So normally, if the user presses the back button (which I would do as an Android user) the user will go back to your app. This the way I would prefer to use for this case, and as far as I know it is also the only way to do this in Android.