Skip to content

Instantly share code, notes, and snippets.

@im182cm
Last active May 28, 2021 02:11
Show Gist options
  • Save im182cm/808407b0b35613be4342c86de32a5882 to your computer and use it in GitHub Desktop.
Save im182cm/808407b0b35613be4342c86de32a5882 to your computer and use it in GitHub Desktop.
While using CustomTabActivity in Facebook SDK, app or service are separated

While using CustomTabActivity in Android Facebook SDK, app or service are separated

Facebook SDK version : 4.28(includes before version which contains CustomTab)

This is what I found while investigating Facebook SDK.
If you have more than one app communicate each other, you can experience this.
There are defence codes in Facebook SDK, so it would not make a problem.

Summary

Set taskAffinity with a package name which holds the task.

<activity
            android:name="com.facebook.CustomTabActivity"
            android:exported="true"
            android:taskAffinity="put your package name here">
            <intent-filter>
                        <action android:name="android.intent.action.VIEW" />
                        <category android:name="android.intent.category.DEFAULT" />
                        <category android:name="android.intent.category.BROWSABLE" />
                        <data android:scheme="@string/fb_login_protocol_scheme" />
            </intent-filter>
</activity>

Login Facebook flow with CustomTab

  1. Facebook Login Button Click
  2. FacebookActivity Create
  3. Attach LoginFragment to FacebookActivity
  4. Start LoginClient to login
  5. Get LoginMethodHandler
  6. Pass handlers with fail, until meet CustomTabLoginMethodHandler
  7. CustomTabLoginMethodHandler calls CustomTabMainActivity
  8. CustomTabMainActivity open CustomTab
  9. Facebook Login in CustomTab
  10. CustomTab redirect to CustomTabActivity
  11. CustomTabActivity calls CustomTabMainActivity to finish CustomTab
  12. CustomTabMainActivity was called on onNewIntent and sendBroadcast to CustomTabActivity and finish with loginresult
  13. Login Success

Normal logs

CustomTabMainActivity onCreate()
CustomTabActivity onCreate()
CustomTabMainActivity onNewIntent()
CustomTabMainActivity onNewIntent REDIRECT ACTION CustomTabMainActivity sendResult()
CustomTabMainActivity onDestroy()
CustomTabActivity onDestroy()

My logs(wrong)

CustomTabMainActivity onCreate()
CustomTabActivity onCreate()
Second CustomTabMainActivity onCreate()
Second CustomTabMainActivity REDIRECT ACTION
CustomTabActivity RESULT_CANCELED
CustomTabActivity redirectReceiver sendBroadcast()
CustomTabMainActivity redirectReceiver onReceived()
CustomTabMainActivity onNewIntent()
CustomTabMainActivity onNewIntent REFRESH ACTION
CustomTabMainActivity closeReceiver sendBroadcast()
CustomTabMainActivity sendResult()
CustomTabActivity closeReceiver onReceived()
CustomTabActivity onDestroy()
CustomTabMainActivity onDestroy()
Second CustomTabMainActivity onDestroy()

Wrong stack

Run #5: ActivityRecord{ab3df4 u0 com.xxx.xxx.xxx/com.facebook.CustomTabMainActivity t332}
Run #4: ActivityRecord{b54c4d4 u0 com.xxx.xxx.xxx/com.facebook.CustomTabActivity t332}
Run #3: ActivityRecord{7df79ae u0 com.android.chrome/org.chromium.chrome.browser.customtabs.CustomTabActivity t331}
Run #2: ActivityRecord{4a5445f u0 com.xxx.xxx.xxx/com.facebook.CustomTabMainActivity t331}
Run #1: ActivityRecord{a27fb03 u0 com.xxx.xxx.xxx/com.facebook.FacebookActivity t331}

Problem reson

CustomTab libraries start activity with FLAG_ACTIVITY_NEW_TASK https://chromium.googlesource.com/chromium/src/+/b8a4837ec4019de8ed1b1810663c6326c59ce33d/chrome/android/java_staging/src/org/chromium/chrome/browser/customtabs/CustomTabActivity.java#290
So what happened to me below.

  1. A app started activity of B app with startActivity() - in A app task
  2. B app started Facebook Activity - in A app task
  3. Facebook Activity started Chrome Custom Tab - in A app task
  4. Chrome Custom Tab started CustomTabActivity with New Task - in A app task
  5. Custom tab started CustomTabMainActivity - in B app task
  6. tasks are different so in CustomTabMainActivity onCreate was called.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment