Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update example app to use two activities #1497

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@
<meta-data
android:name="com.google.android.gms.wallet.api.enabled"
android:value="true" />
<activity android:name=".LaunchActivity" android:exported="true">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To handle deeplink .LaunchActivity should be singleTop otherwise it may cause two instances of the app

Suggested change
<activity android:name=".LaunchActivity" android:exported="true">
<activity android:launchMode="singleTop" android:name=".LaunchActivity" android:exported="true">

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trick with this split into two activities is that deeplinks should be handled by MainActivity directly, not LaunchActivity. MainActivity can then be singleTask or singleTop depending on what you prefer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and kept deeplinks handled by MainActivity with singleTask launchMode (required to prevent issue with 3DS) will cause multiple instances. Using android:launchMode="singleTop" for LaunchActivity will prevent the multiple instances, as the deeplink will route the intent to the existing instance instead of creating a new one.

But I agree that it may depend on the use case you have to handle deeplink. This change is not required, but may be necessary depending on the project configuration.

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
android:launchMode="singleTask"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replacing singleTask by singleTop may improve the app compatibility with deep linking.

Opening a deep link with that launchMode will cause the app to be restarted and may cause the lost of the current state.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

singleTask is what is generally recommended (and the only thing that seems to work well in practice) by Branch.io for instance. As long as the activity that handles deeplinks is singleTask, but the one that handles the launcher intent is not, both the Stripe use case, and the Branch use case seem to work well in the same app at the same time.

android:windowSoftInputMode="adjustResize"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.reactnativestripesdk;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;

public class LaunchActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MainApplication application = (MainApplication) getApplication();
// check that MainActivity is not started yet
if (!application.isActivityInBackStack(MainActivity.class)) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
finish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ protected String getMainComponentName() {
return "main";
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((MainApplication) getApplication()).addActivityToStack(this.getClass());
}

@Override
protected void onDestroy() {
super.onDestroy();
((MainApplication) getApplication()).removeActivityFromStack(this.getClass());
}

/**
* Returns the instance of the {@link ReactActivityDelegate}. There the RootView is created and
* you can specify the rendered you wish to use (Fabric or the older renderer).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.example.reactnativestripesdk.newarchitecture.MainApplicationReactNativeHost;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.ArrayList;
import com.reactnativestripesdk.StripeSdkPackage;

public class MainApplication extends Application implements ReactApplication {
Expand Down Expand Up @@ -60,6 +61,20 @@ public void onCreate() {
initializeFlipper(this, getReactNativeHost().getReactInstanceManager()); // Remove this line if you don't want Flipper enabled
}

private ArrayList<Class> runningActivities = new ArrayList<>();

public void addActivityToStack (Class cls) {
if (!runningActivities.contains(cls)) runningActivities.add(cls);
}

public void removeActivityFromStack (Class cls) {
if (runningActivities.contains(cls)) runningActivities.remove(cls);
}

public boolean isActivityInBackStack (Class cls) {
return runningActivities.contains(cls);
}

/**
* Loads Flipper in React Native templates.
*
Expand Down