Browse Source

Handle deep links when app is created

Guy Carmeli 8 years ago
parent
commit
dd111f53cb

+ 30
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/DeepLinkHandler.java View File

1
+package com.reactnativenavigation.controllers;
2
+
3
+import android.content.Intent;
4
+import android.net.Uri;
5
+
6
+import static android.content.Intent.ACTION_VIEW;
7
+
8
+public class DeepLinkHandler {
9
+    private static Uri deepLink;
10
+
11
+    static void saveDeepLinkData(Uri deepLink) {
12
+        DeepLinkHandler.deepLink = deepLink;
13
+    }
14
+
15
+    static boolean hasDeepLinkData() {
16
+        return deepLink != null;
17
+    }
18
+
19
+    static void setDeepLinkData(Intent intent) {
20
+        if (deepLink != null) {
21
+            intent.setData(deepLink);
22
+            intent.setAction(ACTION_VIEW);
23
+            clear();
24
+        }
25
+    }
26
+
27
+    private static void clear() {
28
+        deepLink = null;
29
+    }
30
+}

+ 0
- 26
android/app/src/main/java/com/reactnativenavigation/controllers/Linking.java View File

1
-package com.reactnativenavigation.controllers;
2
-
3
-import android.content.Intent;
4
-import android.net.Uri;
5
-
6
-import static android.content.Intent.ACTION_VIEW;
7
-
8
-public class Linking {
9
-    private static Uri initialUri;
10
-
11
-    static void saveInitialUri(Uri initialUri) {
12
-        Linking.initialUri = initialUri;
13
-    }
14
-
15
-    static void setInitialUri(Intent intent) {
16
-        if (initialUri != null) {
17
-            intent.setData(initialUri);
18
-            intent.setAction(ACTION_VIEW);
19
-            clear();
20
-        }
21
-    }
22
-
23
-    private static void clear() {
24
-        initialUri = null;
25
-    }
26
-}

+ 7
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java View File

81
         }
81
         }
82
 
82
 
83
         currentActivity = this;
83
         currentActivity = this;
84
+        setDeepLinkData();
84
         NavigationApplication.instance.getReactGateway().onResumeActivity(this, this);
85
         NavigationApplication.instance.getReactGateway().onResumeActivity(this, this);
85
         EventBus.instance.register(this);
86
         EventBus.instance.register(this);
86
     }
87
     }
87
 
88
 
89
+    private void setDeepLinkData() {
90
+        if (DeepLinkHandler.hasDeepLinkData()) {
91
+            DeepLinkHandler.setDeepLinkData(getIntent());
92
+        }
93
+    }
94
+
88
     @Override
95
     @Override
89
     protected void onPause() {
96
     protected void onPause() {
90
         super.onPause();
97
         super.onPause();

+ 1
- 1
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationCommandsHandler.java View File

36
         } else {
36
         } else {
37
             intent = new Intent(NavigationApplication.instance, NavigationActivity.class);
37
             intent = new Intent(NavigationApplication.instance, NavigationActivity.class);
38
         }
38
         }
39
-        Linking.setInitialUri(intent);
39
+        DeepLinkHandler.setDeepLinkData(intent);
40
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
40
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
41
         intent.putExtra(ACTIVITY_PARAMS_BUNDLE, params);
41
         intent.putExtra(ACTIVITY_PARAMS_BUNDLE, params);
42
         NavigationApplication.instance.startActivity(intent);
42
         NavigationApplication.instance.startActivity(intent);

+ 4
- 1
android/app/src/main/java/com/reactnativenavigation/controllers/SplashActivity.java View File

16
     protected void onCreate(@Nullable Bundle savedInstanceState) {
16
     protected void onCreate(@Nullable Bundle savedInstanceState) {
17
         super.onCreate(savedInstanceState);
17
         super.onCreate(savedInstanceState);
18
         setSplashLayout();
18
         setSplashLayout();
19
-        Linking.saveInitialUri(getIntent().getData());
19
+        DeepLinkHandler.saveDeepLinkData(getIntent().getData());
20
     }
20
     }
21
 
21
 
22
     @Override
22
     @Override
24
         super.onResume();
24
         super.onResume();
25
 
25
 
26
         if (NavigationApplication.instance.getReactGateway().hasStartedCreatingContext()) {
26
         if (NavigationApplication.instance.getReactGateway().hasStartedCreatingContext()) {
27
+            if (DeepLinkHandler.hasDeepLinkData()) {
28
+                finish();
29
+            }
27
             return;
30
             return;
28
         }
31
         }
29
 
32