Search code examples
androidonactivityresultandroid-sharing

Start Function After Share Intent Android


I am using the share dialog to share a text in twitter with the code below.

postTweetAfterIntentVar = 1;

Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.setAction(Intent.ACTION_SEND);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "tweet text here");
tweetIntent.setType("text/plain");
startActivity(Intent.createChooser(tweetIntent, "Share this via"));

And I want to start a function after the share operation is finished. I use the code below, but nothing happends after the share operation with the intent is finished. Am I doing anything wrong? Any help would be great.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);


    if (postTweetAfterIntentVar == 1) {

        postTweetAfterIntentVar = 0;

            postTweetFunc();


    }


}

Solution

  • onActivityResult() will not be invoked if you start your activity using startActivity().

    onActivityResult() gets invoked once you start any activity using startActivityFoResult() and not startActivity(). So, when you start activity using startActivityFoResult() the called activity sets the result using setResult() and then you get that result in onActivityResult().