I have empty Splash activity serving as entry point to the Android application and invoking appropriate activities if Branch data is received.
In case of Branch callback error or missing or unrecognized data it invokes default Main activity.
It all works well if device has Internet connectivity, but in case of failure onInitFinished
callback is called twice in a row (once with empty data set and once triggering error), invoking Main activity twice.
public class SplashActivity extends AppCompatActivity
{
Branch.BranchReferralInitListener branchCallback;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_activity);
Log.d("XAPP", "Splash");
branchCallback = new Branch.BranchReferralInitListener()
{
@Override
public void onInitFinished(JSONObject referringParams, BranchError error)
{
Log.d("XAPP", "Branch init session");
if (error == null)
{
Log.d("XAPP", referringParams.toString());
// run different activities depending on the parameters
....
else
{
// fallback to Main activity
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
}
}
else
{
Log.i("XAPP", error.getMessage());
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
}
finish();
}
};
}
@Override
protected void onStart()
{
super.onStart();
Log.d("XAPP", "onStart");
Branch branch = Branch.getInstance();
branch.initSession(branchCallback);
}
}
Resulting logcat when application runs without being connected to the Internet (after it has been previously - at some point - opened through Branch deep link and Branch data has been initialized):
D/XAPP: Splash
D/XAPP: onStart
D/XAPP: Branch init session
D/XAPP: {"+is_first_session":false,"+clicked_branch_link":false}
D/XAPP: Branch init session
I/XAPP: Trouble initializing Branch. Branch API Error: poor network connectivity. Please try again later.
Splash activity is declared as singleTask
activity and is started only once.
Relevant parts of AndroidManifest:
<uses-permission android:name="android.permission.INTERNET"/>
...
<activity
android:name=".SplashActivity"
android:launchMode="singleTask"
android:label="@string/app_name"
android:theme="@style/AppTheme.FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<!-- Branch URI Scheme -->
<intent-filter>
<data android:host="open" android:scheme="xxxx"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.FullScreen">
</activity>
Relevant parts of the Gradle:
android {
compileSdkVersion 26
buildToolsVersion '27.0.3'
defaultConfig {
minSdkVersion 15
targetSdkVersion 26
...
dependencies {
compile('io.branch.sdk.android:library:2.14.4') {
exclude module: 'answers-shim'
}
I could solve the issue by making Main activity singleTask
or removing the Splash activity altogether - by moving branching into Main activity, but those solutions are not viable options in this particular case.
One of the possible solutions would also be adding some boolean flag to recognize onInitFinished
has already been called, but I would like to avoid that one if possible.
My main concern in this situation and actual question is not how to hack the thing to make it work, but why is onInitFinished
called twice and is there a flaw in my Branch callback implementation?