Browse Source

Set Activity contentView in setRoot

This allows users to set splash layouts in the Activities' onCreate method
by passing their splash view to setContentView in onCreate.

```java
public class MainActivity extends NavigationActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View splash = new View(this);
        splash.setBackgroundColor(Color.RED);
        setContentView(splash);
    }
}
```
Guy Carmeli 6 years ago
parent
commit
4adc1485a3

+ 0
- 1
lib/android/app/src/main/java/com/reactnativenavigation/NavigationActivity.java View File

31
         super.onCreate(savedInstanceState);
31
         super.onCreate(savedInstanceState);
32
         navigator = new Navigator(this, new ChildControllersRegistry(), new ModalStack(this), new OverlayManager());
32
         navigator = new Navigator(this, new ChildControllersRegistry(), new ModalStack(this), new OverlayManager());
33
         getReactGateway().onActivityCreated(this);
33
         getReactGateway().onActivityCreated(this);
34
-        setContentView(navigator.getView());
35
     }
34
     }
36
 
35
 
37
     @Override
36
     @Override

+ 3
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/Navigator.java View File

105
 
105
 
106
     public void setRoot(final ViewController viewController, CommandListener commandListener) {
106
     public void setRoot(final ViewController viewController, CommandListener commandListener) {
107
         destroyRoot();
107
         destroyRoot();
108
+        if (view == null) {
109
+            getActivity().setContentView(getView());
110
+        }
108
         root = viewController;
111
         root = viewController;
109
         contentLayout.addView(viewController.getView());
112
         contentLayout.addView(viewController.getView());
110
         if (viewController.options.animations.startApp.hasAnimation()) {
113
         if (viewController.options.animations.startApp.hasAnimation()) {

+ 7
- 2
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/NavigatorTest.java View File

88
         child3 = new SimpleViewController(activity, childRegistry, "child3", tabOptions);
88
         child3 = new SimpleViewController(activity, childRegistry, "child3", tabOptions);
89
         child4 = new SimpleViewController(activity, childRegistry, "child4", tabOptions);
89
         child4 = new SimpleViewController(activity, childRegistry, "child4", tabOptions);
90
         child5 = new SimpleViewController(activity, childRegistry, "child5", tabOptions);
90
         child5 = new SimpleViewController(activity, childRegistry, "child5", tabOptions);
91
-        activity.setContentView(uut.getView());
92
 
91
 
93
         activityController.visible();
92
         activityController.visible();
94
     }
93
     }
105
         verify(modalStack).setDefaultOptions(defaultOptions);
104
         verify(modalStack).setDefaultOptions(defaultOptions);
106
     }
105
     }
107
 
106
 
107
+    @Test
108
+    public void setRoot_setContentViewWhenFirstRootIsSet() {
109
+        assertThat(uut.view).isNull();
110
+        uut.setRoot(child1, new CommandListenerAdapter());
111
+        assertThat(uut.view).isNotNull();
112
+    }
113
+
108
     @Test
114
     @Test
109
     public void setRoot_AddsChildControllerView() {
115
     public void setRoot_AddsChildControllerView() {
110
-        assertThat(uut.getContentLayout().getChildCount()).isZero();
111
         uut.setRoot(child1, new CommandListenerAdapter());
116
         uut.setRoot(child1, new CommandListenerAdapter());
112
         assertIsChild(uut.getContentLayout(), child1.getView());
117
         assertIsChild(uut.getContentLayout(), child1.getView());
113
     }
118
     }