Learning Android Development

Undocumented startActivityForResult behavior for Fragment

Clearing seemingly misunderstood mysterious behavior for many

Picture by Jelleke Vanooteghem on Unsplash

In Android, everyone knows startActivityForResult and onActivityResult as clearly documented by Google in Getting Results from an Activity and Activity.

However, there’s hardly any mention of how it behaves when a Fragment is involved. And due to this, this causes some common bugs on various apps and hard-to-understand behavior.

Check out and ensure you are clear about it, that could save previous future time debugging the strange issue (that I faced in the past 😓)

The basic startActivityForResult Activity flow

Before we get into the undocumented behavior, let’s look at the known behavior. You could skip to the other section if you were already familiar with it.

The entire startActivityForResult, and onActivityResult is to allow a 2-way communication between the source activity and the destination activity. Its flow is as shown in the diagram below.

  • The source activity call, startActivityForResult by sending in the intent together with the requestCode to Android SDK.

--

--