BranchMetrics / android-branch-deep-linking-attribution

The Branch Android SDK for deep linking and attribution. Branch helps mobile apps grow with deep links / deeplinks that power paid acquisition and re-engagement campaigns, referral programs, content sharing, deep linked emails, smart banners, custom user onboarding, and more.
https://docs.branch.io/pages/apps/android/
MIT License
400 stars 156 forks source link

Does initSession() needs to be called in every activity's onResume? #803

Open amankgo opened 4 years ago

amankgo commented 4 years ago

In reference to #764 I have a quick question: Why do you need to call initSession() in every Activity's onResume (BranchActivityLifecycleObserver)? I can understand for the launcher activities it is ok, but for all other activities, it doesn't seem necessary. Can't we have some kind of additional control which we can use to turn on and off this initSession() call in onResume.

We don't want unwanted calling of this method which is causing performance numbers being increased in our app. Possible things to do is to give some flag to enable disable initSession() call in BranchActivityLifecycleObserver. Or if you can optimise the below methods, that would be the best solution.

Following are the screenshots for time initSession() takes: 1.) On release build with minimum overhead (Most realistic estimate)

Screenshot 2020-01-30 at 11 51 25 PM

2.) On Debug build, so that you can guess the methods in previous obfuscated code:

Screenshot 2020-01-30 at 11 53 08 PM
bklastaitis-branch commented 4 years ago

@amankgo which version of the SDK are you using?

amankgo commented 4 years ago

@bklastaitis-branch 4.2.0 I don't see any changes related to this in the latest versions.

amankgo commented 4 years ago

Any update on this?

bklastaitis-branch commented 4 years ago

Hi @amankgo, sorry the delayed communication. In v4.2.0 and above Branch only calls initSession if session has not been initialized yet. Which should be done by the user inLauncherActivity.onStart. It is a way for us to capture new sessions when user launches the app from the recent apps list and the last used Activity was not LauncherActivity.

        if (branch.getInitState() == Branch.SESSION_STATE.UNINITIALISED) {
            // this is the only place where we self-initialize in case user opens the app from 'recent apps tray'
            // and the entry Activity is not the launcher Activity where user placed initSession themselves.
            branch.initSession(activity);
        }

Do you see this method being called even after you call initSession yourself (e.g. as you navigate to the next activity and it reaches the RESUMED state)?

amankgo commented 4 years ago

Yeah... but if you see "close session", it changes it's state to Branch.SESSION_STATE.UNINITIALISED. So Next time when I resume the activity, it will call the initSession() again. This causes initSession() to call mostly everytime. Can we not optimise this somehow?

bklastaitis-branch commented 4 years ago

Hmm.. So it is the intended behavior to call initSession every time the app is launched, whether it's done via clicking a Branch link or the app icon or (re)launching the app via the recent apps list. But we expect it to be done just once and further navigation between activities/fragments is irrelevant with regards to session creation/closing (with some very specific exceptions like intra-app linking). We do not currently plan to revise this behavior.

However, if you see observe initSession being called multiple times because session gets closed while app is still in foreground (or any other reason), please send us a code snippet/stacktrace/sample project, so we can investigate.