Browse 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 8 years ago
parent
commit
39548c57e6

+ 6
- 6
android/app/src/main/java/com/reactnativenavigation/activities/BaseReactActivity.java View File

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

+ 15
- 26
android/app/src/main/java/com/reactnativenavigation/core/RctManager.java View File

1
 package com.reactnativenavigation.core;
1
 package com.reactnativenavigation.core;
2
 
2
 
3
 import android.app.Application;
3
 import android.app.Application;
4
-import android.content.Context;
5
 
4
 
6
 import com.facebook.react.LifecycleState;
5
 import com.facebook.react.LifecycleState;
7
 import com.facebook.react.ReactInstanceManager;
6
 import com.facebook.react.ReactInstanceManager;
9
 import com.facebook.react.bridge.ReactContext;
8
 import com.facebook.react.bridge.ReactContext;
10
 import com.facebook.react.bridge.ReactContextBaseJavaModule;
9
 import com.facebook.react.bridge.ReactContextBaseJavaModule;
11
 import com.facebook.react.bridge.WritableMap;
10
 import com.facebook.react.bridge.WritableMap;
11
+import com.reactnativenavigation.activities.BaseReactActivity;
12
 import com.reactnativenavigation.core.objects.Screen;
12
 import com.reactnativenavigation.core.objects.Screen;
13
 
13
 
14
-import java.util.List;
15
-
16
 import static com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
14
 import static com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
17
 
15
 
18
 /**
16
 /**
23
     private static RctManager sInstance;
21
     private static RctManager sInstance;
24
 
22
 
25
     private ReactInstanceManager mReactManager;
23
     private ReactInstanceManager mReactManager;
26
-    private boolean mUseLocalDevServer = true;
27
 
24
 
28
     private RctManager() {
25
     private RctManager() {
29
         // Singleton
26
         // Singleton
44
         return mReactManager != null;
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
      * Creates a React Instance Manager associated with this component name
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
         ReactInstanceManager.Builder builder = ReactInstanceManager.builder()
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
                 .setInitialLifecycleState(LifecycleState.BEFORE_RESUME);
56
                 .setInitialLifecycleState(LifecycleState.BEFORE_RESUME);
63
-        for (ReactPackage reactPackage : packages) {
57
+
58
+        for (ReactPackage reactPackage : reactActivity.getPackages()) {
64
             builder.addPackage(reactPackage);
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
         } else {
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
         mReactManager = builder.build();
70
         mReactManager = builder.build();

+ 15
- 7
android/app/src/main/java/com/reactnativenavigation/core/objects/Button.java View File

24
 public class Button extends JsonObject implements Serializable {
24
 public class Button extends JsonObject implements Serializable {
25
     private static final long serialVersionUID = -570145217281069067L;
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
     private static final String KEY_ID = "id";
28
     private static final String KEY_ID = "id";
30
     private static final String KEY_TITLE = "title";
29
     private static final String KEY_TITLE = "title";
31
     private static final String KEY_ICON = "icon";
30
     private static final String KEY_ICON = "icon";
32
 
31
 
32
+    private static ResourceDrawableIdHelper sResDrawableIdHelper = new ResourceDrawableIdHelper();
33
+
33
     public String id;
34
     public String id;
34
     public String title;
35
     public String title;
35
     private String mIconSource;
36
     private String mIconSource;
53
         }
54
         }
54
 
55
 
55
         try {
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
         } catch (Exception e) {
68
         } catch (Exception e) {
61
             if (BuildConfig.DEBUG) {
69
             if (BuildConfig.DEBUG) {
62
                 e.printStackTrace();
70
                 e.printStackTrace();
75
                     ret = null;
83
                     ret = null;
76
                 }
84
                 }
77
             } catch (Exception e) {
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
             if (ret == null) {
88
             if (ret == null) {
81
                 ret = sResDrawableIdHelper.getResourceDrawableUri(context, mIconSource);
89
                 ret = sResDrawableIdHelper.getResourceDrawableUri(context, mIconSource);

+ 6
- 0
android/app/src/main/java/com/reactnativenavigation/modules/RctActivityModule.java View File

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

+ 3
- 3
android/app/src/main/java/com/reactnativenavigation/utils/ContextProvider.java View File

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