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,7 +31,6 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
31 31
         super.onCreate(savedInstanceState);
32 32
         navigator = new Navigator(this, new ChildControllersRegistry(), new ModalStack(this), new OverlayManager());
33 33
         getReactGateway().onActivityCreated(this);
34
-        setContentView(navigator.getView());
35 34
     }
36 35
 
37 36
     @Override

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

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

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

@@ -88,7 +88,6 @@ public class NavigatorTest extends BaseTest {
88 88
         child3 = new SimpleViewController(activity, childRegistry, "child3", tabOptions);
89 89
         child4 = new SimpleViewController(activity, childRegistry, "child4", tabOptions);
90 90
         child5 = new SimpleViewController(activity, childRegistry, "child5", tabOptions);
91
-        activity.setContentView(uut.getView());
92 91
 
93 92
         activityController.visible();
94 93
     }
@@ -105,9 +104,15 @@ public class NavigatorTest extends BaseTest {
105 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 114
     @Test
109 115
     public void setRoot_AddsChildControllerView() {
110
-        assertThat(uut.getContentLayout().getChildCount()).isZero();
111 116
         uut.setRoot(child1, new CommandListenerAdapter());
112 117
         assertIsChild(uut.getContentLayout(), child1.getView());
113 118
     }