Parcourir la source

Don't keep activities in history stack + enable resources in release mode

From now on, calling start*Application will clear the activity stack and the new activity as
the root of an empty stack. This means we don't support "Modal" or opening screens in new activity.

Enabled resources (Toolbar icons) in release mode.
Guy Carmeli il y a 8 ans
Parent
révision
39548c57e6

+ 6
- 6
android/app/src/main/java/com/reactnativenavigation/activities/BaseReactActivity.java Voir le fichier

@@ -58,7 +58,7 @@ public abstract class BaseReactActivity extends AppCompatActivity implements Def
58 58
      * e.g. "index.android.bundle"
59 59
      */
60 60
     @Nullable
61
-    protected String getBundleAssetName() {
61
+    public String getBundleAssetName() {
62 62
         return "index.android.bundle";
63 63
     }
64 64
 
@@ -69,7 +69,7 @@ public abstract class BaseReactActivity extends AppCompatActivity implements Def
69 69
      * e.g. "file://sdcard/myapp_cache/index.android.bundle"
70 70
      */
71 71
     @Nullable
72
-    protected String getJSBundleFile() {
72
+    public String getJSBundleFile() {
73 73
         return null;
74 74
     }
75 75
 
@@ -79,7 +79,7 @@ public abstract class BaseReactActivity extends AppCompatActivity implements Def
79 79
      * This is the first file to be executed once the {@link ReactInstanceManager} is created.
80 80
      * e.g. "index.android"
81 81
      */
82
-    protected String getJSMainModuleName() {
82
+    public String getJSMainModuleName() {
83 83
         return "index.android";
84 84
     }
85 85
 
@@ -107,7 +107,7 @@ public abstract class BaseReactActivity extends AppCompatActivity implements Def
107 107
     /**
108 108
      * Returns whether dev mode should be enabled. This enables e.g. the dev menu.
109 109
      */
110
-    protected boolean getUseDeveloperSupport() {
110
+    public boolean getUseDeveloperSupport() {
111 111
         return BuildConfig.DEBUG;
112 112
     }
113 113
 
@@ -117,7 +117,7 @@ public abstract class BaseReactActivity extends AppCompatActivity implements Def
117 117
      * If your app uses additional views or modules besides the default ones,
118 118
      * you'll want to include more packages here.
119 119
      */
120
-    protected List<ReactPackage> getPackages() {
120
+    public List<ReactPackage> getPackages() {
121 121
         return Arrays.asList(
122 122
                 new MainReactPackage(),
123 123
                 new RnnPackage()
@@ -148,7 +148,7 @@ public abstract class BaseReactActivity extends AppCompatActivity implements Def
148 148
     protected ReactInstanceManager getReactInstanceManager() {
149 149
         RctManager rctManager = RctManager.getInstance();
150 150
         if (!rctManager.isInitialized()) {
151
-            rctManager.init(getApplicationContext(), getMainComponentName(), getPackages());
151
+            rctManager.init(this);
152 152
         }
153 153
         return rctManager.getReactInstanceManager();
154 154
     }

+ 15
- 26
android/app/src/main/java/com/reactnativenavigation/core/RctManager.java Voir le fichier

@@ -1,7 +1,6 @@
1 1
 package com.reactnativenavigation.core;
2 2
 
3 3
 import android.app.Application;
4
-import android.content.Context;
5 4
 
6 5
 import com.facebook.react.LifecycleState;
7 6
 import com.facebook.react.ReactInstanceManager;
@@ -9,10 +8,9 @@ import com.facebook.react.ReactPackage;
9 8
 import com.facebook.react.bridge.ReactContext;
10 9
 import com.facebook.react.bridge.ReactContextBaseJavaModule;
11 10
 import com.facebook.react.bridge.WritableMap;
11
+import com.reactnativenavigation.activities.BaseReactActivity;
12 12
 import com.reactnativenavigation.core.objects.Screen;
13 13
 
14
-import java.util.List;
15
-
16 14
 import static com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
17 15
 
18 16
 /**
@@ -23,7 +21,6 @@ public class RctManager {
23 21
     private static RctManager sInstance;
24 22
 
25 23
     private ReactInstanceManager mReactManager;
26
-    private boolean mUseLocalDevServer = true;
27 24
 
28 25
     private RctManager() {
29 26
         // Singleton
@@ -44,38 +41,30 @@ public class RctManager {
44 41
         return mReactManager != null;
45 42
     }
46 43
 
47
-    public void init(Context context, String componentName, List<ReactPackage> packages) {
48
-        createReactInstanceManager(context, componentName, packages);
44
+    public void init(BaseReactActivity context) {
45
+        createReactInstanceManager(context);
49 46
     }
50 47
 
51 48
     /**
52 49
      * Creates a React Instance Manager associated with this component name
53
-     * @param context
54
-     * @param jsBundleName
55
-     * @param packages
56 50
      */
57
-    public ReactInstanceManager createReactInstanceManager(Context context, String jsBundleName, List<ReactPackage> packages) {
58
-        // Get component name
51
+    public ReactInstanceManager createReactInstanceManager(BaseReactActivity reactActivity) {
59 52
         ReactInstanceManager.Builder builder = ReactInstanceManager.builder()
60
-                .setApplication((Application) context.getApplicationContext())
61
-                .setUseDeveloperSupport(true)
53
+                .setApplication((Application) reactActivity.getApplicationContext())
54
+                .setJSMainModuleName(reactActivity.getJSMainModuleName())
55
+                .setUseDeveloperSupport(reactActivity.getUseDeveloperSupport())
62 56
                 .setInitialLifecycleState(LifecycleState.BEFORE_RESUME);
63
-        for (ReactPackage reactPackage : packages) {
57
+
58
+        for (ReactPackage reactPackage : reactActivity.getPackages()) {
64 59
             builder.addPackage(reactPackage);
65 60
         }
66
-        if (mUseLocalDevServer) {
67
-            // Set module name to be loaded from local node server
68
-            builder.setJSMainModuleName("index.android");
69
-            builder.setBundleAssetName("index.android.bundle");
61
+
62
+        String jsBundleFile = reactActivity.getJSBundleFile();
63
+
64
+        if (jsBundleFile != null) {
65
+            builder.setJSBundleFile(jsBundleFile);
70 66
         } else {
71
-            throw new RuntimeException("Implement me!");
72
-            //            // Get the bundle uri
73
-            //            String confResourceName = AssetManager.getConfUrlResourceName(jsBundleName);
74
-            //            Context context = AppDelegate.sharedApplication().getApplicationContext();
75
-            //            Uri uri =
76
-            //                    AssetManager.sharedAssetManager().getConfResourceUrl(confResourceName, context);
77
-            //            // Load bundled jsBundle
78
-            //            builder.setJSBundleFile(uri.getPath());
67
+            builder.setBundleAssetName(reactActivity.getBundleAssetName());
79 68
         }
80 69
 
81 70
         mReactManager = builder.build();

+ 15
- 7
android/app/src/main/java/com/reactnativenavigation/core/objects/Button.java Voir le fichier

@@ -24,12 +24,13 @@ import java.util.concurrent.atomic.AtomicInteger;
24 24
 public class Button extends JsonObject implements Serializable {
25 25
     private static final long serialVersionUID = -570145217281069067L;
26 26
 
27
-    private static ResourceDrawableIdHelper sResDrawableIdHelper = new ResourceDrawableIdHelper();
28
-
27
+    public static final String LOCAL_RESOURCE_URI_SCHEME = "res";
29 28
     private static final String KEY_ID = "id";
30 29
     private static final String KEY_TITLE = "title";
31 30
     private static final String KEY_ICON = "icon";
32 31
 
32
+    private static ResourceDrawableIdHelper sResDrawableIdHelper = new ResourceDrawableIdHelper();
33
+
33 34
     public String id;
34 35
     public String title;
35 36
     private String mIconSource;
@@ -53,10 +54,17 @@ public class Button extends JsonObject implements Serializable {
53 54
         }
54 55
 
55 56
         try {
56
-            Uri icon = getIconUri(ctx);
57
-            URL url = new URL(icon.toString());
58
-            Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
59
-            return new BitmapDrawable(bitmap);
57
+            Drawable icon;
58
+            Uri iconUri = getIconUri(ctx);
59
+
60
+            if (LOCAL_RESOURCE_URI_SCHEME.equals(iconUri.getScheme())) {
61
+                icon = sResDrawableIdHelper.getResourceDrawable(ctx, mIconSource);
62
+            } else {
63
+                URL url = new URL(iconUri.toString());
64
+                Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
65
+                icon = new BitmapDrawable(bitmap);
66
+            }
67
+            return icon;
60 68
         } catch (Exception e) {
61 69
             if (BuildConfig.DEBUG) {
62 70
                 e.printStackTrace();
@@ -75,7 +83,7 @@ public class Button extends JsonObject implements Serializable {
75 83
                     ret = null;
76 84
                 }
77 85
             } catch (Exception e) {
78
-                // ignore malformed uri, then attempt to extract resource ID.
86
+                // Ignore malformed uri, then attempt to extract resource ID.
79 87
             }
80 88
             if (ret == null) {
81 89
                 ret = sResDrawableIdHelper.getResourceDrawableUri(context, mIconSource);

+ 6
- 0
android/app/src/main/java/com/reactnativenavigation/modules/RctActivityModule.java Voir le fichier

@@ -37,9 +37,12 @@ public class RctActivityModule extends ReactContextBaseJavaModule {
37 37
         Activity context = ContextProvider.getActivityContext();
38 38
         if (context != null && !context.isFinishing()) {
39 39
             Intent intent = new Intent(context, TabActivity.class);
40
+            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
41
+
40 42
             Bundle extras = new Bundle();
41 43
             extras.putSerializable(TabActivity.EXTRA_SCREENS, createScreens(screens));
42 44
             intent.putExtras(extras);
45
+            
43 46
             context.startActivity(intent);
44 47
         }
45 48
     }
@@ -57,9 +60,12 @@ public class RctActivityModule extends ReactContextBaseJavaModule {
57 60
         BaseReactActivity context = ContextProvider.getActivityContext();
58 61
         if (context != null && !context.isFinishing()) {
59 62
             Intent intent = new Intent(context, SingleScreenActivity.class);
63
+            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
64
+
60 65
             Bundle extras = new Bundle();
61 66
             extras.putSerializable(SingleScreenActivity.EXTRA_SCREEN, new Screen(screen));
62 67
             intent.putExtras(extras);
68
+
63 69
             context.startActivity(intent);
64 70
         }
65 71
     }

+ 3
- 3
android/app/src/main/java/com/reactnativenavigation/utils/ContextProvider.java Voir le fichier

@@ -1,7 +1,5 @@
1 1
 package com.reactnativenavigation.utils;
2 2
 
3
-import android.support.annotation.Nullable;
4
-
5 3
 import com.reactnativenavigation.activities.BaseReactActivity;
6 4
 
7 5
 import java.lang.ref.WeakReference;
@@ -18,7 +16,9 @@ public class ContextProvider {
18 16
         }
19 17
     }
20 18
 
21
-    @Nullable
19
+    /**
20
+     * Returns the currently resumed activity or {@code null} if there is none.
21
+     */
22 22
     public static BaseReactActivity getActivityContext() {
23 23
         return sActivityWR != null ? sActivityWR.get() : null;
24 24
     }