|
@@ -0,0 +1,107 @@
|
|
1
|
+package com.reactnativenavigation.screens;
|
|
2
|
+
|
|
3
|
+import android.app.Fragment;
|
|
4
|
+import android.app.FragmentManager;
|
|
5
|
+import android.app.FragmentTransaction;
|
|
6
|
+import android.os.Bundle;
|
|
7
|
+import android.support.annotation.Nullable;
|
|
8
|
+import android.support.v7.app.AppCompatActivity;
|
|
9
|
+import android.widget.FrameLayout;
|
|
10
|
+
|
|
11
|
+import com.reactnativenavigation.params.ScreenParams;
|
|
12
|
+import com.reactnativenavigation.utils.ViewUtils;
|
|
13
|
+import com.reactnativenavigation.views.TitleBarBackButtonListener;
|
|
14
|
+
|
|
15
|
+import java.lang.reflect.InvocationTargetException;
|
|
16
|
+import java.lang.reflect.Method;
|
|
17
|
+
|
|
18
|
+import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
|
|
19
|
+
|
|
20
|
+@SuppressWarnings("ResourceType")
|
|
21
|
+public class FragmentScreen extends Screen {
|
|
22
|
+
|
|
23
|
+ private static final String CONTRACT_GET_FRAGMENT = "getFragment";
|
|
24
|
+ private static final String CONTRACT_GET_SUPPORT_FRAGMENT = "getSupportFragment";
|
|
25
|
+ private final AppCompatActivity activity;
|
|
26
|
+ private FrameLayout content;
|
|
27
|
+
|
|
28
|
+ public FragmentScreen(AppCompatActivity activity, ScreenParams screenParams, TitleBarBackButtonListener titleBarBackButtonListener) {
|
|
29
|
+ super(activity, screenParams, titleBarBackButtonListener);
|
|
30
|
+ this.activity = activity;
|
|
31
|
+ }
|
|
32
|
+
|
|
33
|
+ @Override
|
|
34
|
+ protected void createContent() {
|
|
35
|
+ content = new FrameLayout(getContext());
|
|
36
|
+ content.setId(ViewUtils.generateViewId());
|
|
37
|
+ addView(content, MATCH_PARENT, MATCH_PARENT);
|
|
38
|
+ addFragment();
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ private void addFragment() {
|
|
42
|
+ try {
|
|
43
|
+ Fragment fragment = tryGetFragment();
|
|
44
|
+ if (fragment != null) {
|
|
45
|
+ addFragment(fragment);
|
|
46
|
+ return;
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ android.support.v4.app.Fragment supportFragment = tryGetSupportFragment();
|
|
50
|
+ if (supportFragment != null) {
|
|
51
|
+ addSupportFragment(supportFragment);
|
|
52
|
+ return;
|
|
53
|
+ }
|
|
54
|
+ throw new RuntimeException("must provide public static method " + CONTRACT_GET_FRAGMENT + " or " + CONTRACT_GET_SUPPORT_FRAGMENT);
|
|
55
|
+ } catch (Exception e) {
|
|
56
|
+ throw new RuntimeException(e);
|
|
57
|
+ }
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ private void addFragment(Fragment fragment) {
|
|
61
|
+ FragmentManager fm = activity.getFragmentManager();
|
|
62
|
+ FragmentTransaction transaction = fm.beginTransaction();
|
|
63
|
+ transaction.add(content.getId(), fragment);
|
|
64
|
+ transaction.commit();
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ private void addSupportFragment(android.support.v4.app.Fragment supportFragment) {
|
|
68
|
+ android.support.v4.app.FragmentManager fm = activity.getSupportFragmentManager();
|
|
69
|
+ android.support.v4.app.FragmentTransaction transaction = fm.beginTransaction();
|
|
70
|
+ transaction.add(content.getId(), supportFragment);
|
|
71
|
+ transaction.commit();
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ @Nullable
|
|
75
|
+ private Fragment tryGetFragment() throws ClassNotFoundException, IllegalAccessException, InvocationTargetException {
|
|
76
|
+ try {
|
|
77
|
+ String className = screenParams.fragmentCreatorClassName;
|
|
78
|
+ Class<?> fragmentCreatorClass = Class.forName(className);
|
|
79
|
+ Method method = fragmentCreatorClass.getMethod(CONTRACT_GET_FRAGMENT, Bundle.class);
|
|
80
|
+ return (android.app.Fragment) method.invoke(null, screenParams.passProps);
|
|
81
|
+ } catch (NoSuchMethodException noSuchMethod) {
|
|
82
|
+ return null;
|
|
83
|
+ }
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ @Nullable
|
|
87
|
+ private android.support.v4.app.Fragment tryGetSupportFragment() throws ClassNotFoundException, IllegalAccessException, InvocationTargetException {
|
|
88
|
+ try {
|
|
89
|
+ String className = screenParams.fragmentCreatorClassName;
|
|
90
|
+ Class<?> fragmentCreatorClass = Class.forName(className);
|
|
91
|
+ Method method = fragmentCreatorClass.getMethod(CONTRACT_GET_SUPPORT_FRAGMENT, Bundle.class);
|
|
92
|
+ return (android.support.v4.app.Fragment) method.invoke(null, screenParams.passProps);
|
|
93
|
+ } catch (NoSuchMethodException noSuchMethod) {
|
|
94
|
+ return null;
|
|
95
|
+ }
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ @Override
|
|
99
|
+ public void ensureUnmountOnDetachedFromWindow() {
|
|
100
|
+ // nothing
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+ @Override
|
|
104
|
+ public void preventUnmountOnDetachedFromWindow() {
|
|
105
|
+ // nothing
|
|
106
|
+ }
|
|
107
|
+}
|