ソースを参照

Handle deep links when app is created

Guy Carmeli 8 年 前
コミット
dd111f53cb

+ 30
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/DeepLinkHandler.java ファイルの表示

@@ -0,0 +1,30 @@
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 ファイルの表示

@@ -1,26 +0,0 @@
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 ファイルの表示

@@ -81,10 +81,17 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
81 81
         }
82 82
 
83 83
         currentActivity = this;
84
+        setDeepLinkData();
84 85
         NavigationApplication.instance.getReactGateway().onResumeActivity(this, this);
85 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 95
     @Override
89 96
     protected void onPause() {
90 97
         super.onPause();

+ 1
- 1
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationCommandsHandler.java ファイルの表示

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

+ 4
- 1
android/app/src/main/java/com/reactnativenavigation/controllers/SplashActivity.java ファイルの表示

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