Rich-text Reply

Android - startOptimizelyAsync() not triggering onOptimizelyStarted() when loaded from deep link

yuval 05-28-15
Accepted Solution

Android - startOptimizelyAsync() not triggering onOptimizelyStarted() when loaded from deep link

[ Edited ]

In my app I am using `startOptimizelyAsync()` in my Application#onCreate() to start Optimizely. I am listening for the `onOptimizelyStarted()` callback, as recommended by this post, and setting all my Live Variables in it, like so:

 

 

@Nullable private static LiveVariable<Boolean> myLiveVariable = null;

public void onCreate() {
    super.onCreate();

    Optimizely.startOptimizelyAsync(TOKEN_PROD, getApp(), new SimpleOptimizelyEventListener() {
        @Override
        public void onOptimizelyStarted() {
            Log.d(TAG, "Optimizely Started");
            loadLiveVariables();
        }
    });
}

public static void loadLiveVariables() {
    myLiveVariable = Optimizely.booleanVariable("myLiveVariable", false);
}

 

 

This has been working very well for most cases, but fails in this edge case:

 

1. Kill the app process by swiping it away from recents

2. Open the app through a deep link, e.g. from a notification or a web link in an email or web browser

 

What I've noticed is that Optimizely is never started. `startOptimizelyAsync()` is definitely called, but the `onOptimizelyStarted()` callback is never hit, and I know this because the "Optimizely Started" log message I put in there is never printed.

 

To try to debug this, I tried putting similar log messages into every method of OptimizelyEventListener. I was hoping that onOptimizelyFailedToStart() would give me some feedback as to the error - but nope, my log call in there was never invoked either. In fact, it doesn't seem that any of the logging messages except for onOptimizelyStarted() or onOptimizelyDataFileLoaded() are ever invoked at all - which is probably a separate bug from this deep link issue.

 

I tried to start optimizely syncrhonously instead of asynchrnously - this did, in fact, fix the deep link problem. But it is not a solution for me because I definitely want to start Optimizely asynchronously.

 

So to summarize:

1. Why won't Optimizely start correctly when started asynchronously when the app is created from a deep link?

2. Why aren't the OptimizelyEventListener callbacks being invoked?

 

Thank you!

 

Level 2

Re: Android - startOptimizelyAsync() not triggering onOptimizelyStarted() when loaded from deep link

Hi!

Thanks for the detailed summary of this issue! I believe your listener may be getting garbage collected before startOptimizely finishes. We hold a weak reference to the listener, so it's up to you to hold a strong reference to the listener. (see http://developers.optimizely.com/android/help/reference/com/optimizely/Optimizely.html#addOptimizely... ) for more info.

Can you try binding your listener to a field in your application class?
Optimizely