Browse Source

fake onNewIntent to handle deep links when app is in foreground (#766)

Guy Carmeli 8 years ago
parent
commit
7367a8dba1

+ 39
- 5
android/app/src/main/java/com/reactnativenavigation/controllers/IntentDataHandler.java View File

@@ -1,26 +1,60 @@
1 1
 package com.reactnativenavigation.controllers;
2 2
 
3 3
 import android.content.Intent;
4
+import android.support.annotation.Nullable;
5
+
6
+import com.reactnativenavigation.NavigationApplication;
4 7
 
5 8
 import static android.content.Intent.ACTION_VIEW;
6 9
 
7
-public class IntentDataHandler {
10
+class IntentDataHandler {
8 11
     private static Intent intent;
9 12
 
13
+    static void onStartApp(Intent intent) {
14
+        setIntentData(intent);
15
+    }
16
+
17
+    static void onResume(Intent intent) {
18
+        if (hasIntentData()) {
19
+            setIntentData(intent);
20
+        } else {
21
+            saveIntentData(intent);
22
+        }
23
+    }
24
+
10 25
     static void saveIntentData(Intent intent) {
11 26
         IntentDataHandler.intent = intent;
12 27
     }
13 28
 
14
-    static boolean hasIntentData() {
29
+    static void onPostResume(Intent intent) {
30
+        if (hasIntentData()) {
31
+            fakeOnNewIntentForLinkingModule(intent);
32
+            clear();
33
+        }
34
+    }
35
+
36
+    static void onPause(@Nullable Intent intent) {
37
+        if (intent != null) {
38
+            intent.setData(null);
39
+        }
40
+        clear();
41
+    }
42
+
43
+    private static void fakeOnNewIntentForLinkingModule(Intent intent) {
44
+        if (intent != null) {
45
+            NavigationApplication.instance.getReactGateway().onNewIntent(intent);
46
+        }
47
+    }
48
+
49
+    private static boolean hasIntentData() {
15 50
         return intent != null;
16 51
     }
17 52
 
18
-    static void setIntentData(Intent intent) {
19
-        if (IntentDataHandler.intent != null) {
53
+    private static void setIntentData(@Nullable Intent intent) {
54
+        if (intent != null && IntentDataHandler.intent != null) {
20 55
             intent.setData(IntentDataHandler.intent.getData());
21 56
             intent.putExtras(IntentDataHandler.intent);
22 57
             intent.setAction(ACTION_VIEW);
23
-            clear();
24 58
         }
25 59
     }
26 60
 

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

@@ -106,16 +106,11 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
106 106
         }
107 107
 
108 108
         currentActivity = this;
109
-        setDeepLinkData();
109
+        IntentDataHandler.onResume(getIntent());
110 110
         NavigationApplication.instance.getReactGateway().onResumeActivity(this, this);
111 111
         NavigationApplication.instance.getActivityCallbacks().onActivityResumed(this);
112 112
         EventBus.instance.register(this);
113
-    }
114
-
115
-    private void setDeepLinkData() {
116
-        if (IntentDataHandler.hasIntentData()) {
117
-            IntentDataHandler.setIntentData(getIntent());
118
-        }
113
+        IntentDataHandler.onPostResume(getIntent());
119 114
     }
120 115
 
121 116
     @Override
@@ -129,6 +124,7 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
129 124
     protected void onPause() {
130 125
         super.onPause();
131 126
         currentActivity = null;
127
+        IntentDataHandler.onPause(getIntent());
132 128
         NavigationApplication.instance.getReactGateway().onPauseActivity();
133 129
         NavigationApplication.instance.getActivityCallbacks().onActivityPaused(this);
134 130
         EventBus.instance.unregister(this);

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

@@ -42,7 +42,7 @@ public class NavigationCommandsHandler {
42 42
         } else {
43 43
             intent = new Intent(NavigationApplication.instance, NavigationActivity.class);
44 44
         }
45
-        IntentDataHandler.setIntentData(intent);
45
+        IntentDataHandler.onStartApp(intent);
46 46
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
47 47
         intent.putExtra(ACTIVITY_PARAMS_BUNDLE, params);
48 48
         NavigationApplication.instance.startActivity(intent);