Skip to content

Commit 3a26891

Browse files
committed
Add: add sample code
1 parent 23bd58f commit 3a26891

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1624
-132
lines changed

android/app/build.gradle

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,9 @@ import com.android.build.OutputFile
6969
*
7070
* // supply additional arguments to the packager
7171
* extraPackagerArgs: []
72-
* ]
73-
*/
72+
* ]*/
7473

75-
project.ext.react = [
76-
entryFile: "index.js"
77-
]
74+
project.ext.react = [entryFile: "index.js"]
7875

7976
apply from: "../../node_modules/react-native/react.gradle"
8077

@@ -84,67 +81,67 @@ apply from: "../../node_modules/react-native/react.gradle"
8481
* - An APK that only works on x86 devices
8582
* The advantage is the size of the APK is reduced by about 4MB.
8683
* Upload all the APKs to the Play Store and people will download
87-
* the correct one based on the CPU architecture of their device.
88-
*/
84+
* the correct one based on the CPU architecture of their device.*/
8985
def enableSeparateBuildPerCPUArchitecture = false
9086

9187
/**
92-
* Run Proguard to shrink the Java bytecode in release builds.
93-
*/
88+
* Run Proguard to shrink the Java bytecode in release builds.*/
9489
def enableProguardInReleaseBuilds = false
9590

9691
android {
97-
compileSdkVersion 25
98-
buildToolsVersion "25.0.3"
92+
compileSdkVersion 25
93+
buildToolsVersion "25.0.3"
9994

100-
defaultConfig {
101-
applicationId "com.reactnativenavigationdemo"
102-
minSdkVersion 16
103-
targetSdkVersion 16
104-
versionCode 1
105-
versionName "1.0"
106-
ndk {
107-
abiFilters "armeabi-v7a", "x86"
108-
}
95+
defaultConfig {
96+
applicationId "com.reactnativenavigationdemo"
97+
minSdkVersion 16
98+
targetSdkVersion 16
99+
versionCode 1
100+
versionName "1.0"
101+
ndk {
102+
abiFilters "armeabi-v7a", "x86"
109103
}
110-
splits {
111-
abi {
112-
reset()
113-
enable enableSeparateBuildPerCPUArchitecture
114-
universalApk false // If true, also generate a universal APK
115-
include "armeabi-v7a", "x86"
116-
}
104+
}
105+
splits {
106+
abi {
107+
reset()
108+
enable enableSeparateBuildPerCPUArchitecture
109+
universalApk false // If true, also generate a universal APK
110+
include "armeabi-v7a", "x86"
117111
}
118-
buildTypes {
119-
release {
120-
minifyEnabled enableProguardInReleaseBuilds
121-
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
122-
}
112+
}
113+
buildTypes {
114+
release {
115+
minifyEnabled enableProguardInReleaseBuilds
116+
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
123117
}
124-
// applicationVariants are e.g. debug, release
125-
applicationVariants.all { variant ->
126-
variant.outputs.each { output ->
127-
// For each separate APK per architecture, set a unique version code as described here:
128-
// http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
129-
def versionCodes = ["armeabi-v7a":1, "x86":2]
130-
def abi = output.getFilter(OutputFile.ABI)
131-
if (abi != null) { // null for the universal-debug, universal-release variants
132-
output.versionCodeOverride =
133-
versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
134-
}
135-
}
118+
}
119+
// applicationVariants are e.g. debug, release
120+
applicationVariants.all { variant ->
121+
variant.outputs.each { output ->
122+
// For each separate APK per architecture, set a unique version code as described here:
123+
// http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
124+
def versionCodes = ["armeabi-v7a": 1, "x86": 2]
125+
def abi = output.getFilter(OutputFile.ABI)
126+
if (abi != null) {
127+
// null for the universal-debug, universal-release variants
128+
output.versionCodeOverride = versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
129+
}
136130
}
131+
}
137132
}
138133

139134
dependencies {
140-
compile fileTree(dir: "libs", include: ["*.jar"])
141-
compile "com.android.support:appcompat-v7:25.3.1"
142-
compile "com.facebook.react:react-native:+" // From node_modules
135+
compile fileTree(dir: "libs", include: ["*.jar"])
136+
137+
compile "com.android.support:appcompat-v7:25.3.1"
138+
139+
compile "com.facebook.react:react-native:+" // From node_modules
143140
}
144141

145142
// Run this once to be able to run the application with BUCK
146143
// puts all compile dependencies into folder libs for BUCK to use
147144
task copyDownloadableDepsToLibs(type: Copy) {
148-
from configurations.compile
149-
into 'libs'
150-
}
145+
from configurations.compile
146+
into 'libs'
147+
}
Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.reactnativenavigationdemo"
3-
android:versionCode="1"
4-
android:versionName="1.0">
2+
package="com.reactnativenavigationdemo">
53

6-
<uses-permission android:name="android.permission.INTERNET" />
7-
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
4+
<uses-permission android:name="android.permission.INTERNET"/>
5+
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
86

9-
<uses-sdk
10-
android:minSdkVersion="16"
11-
android:targetSdkVersion="22" />
12-
13-
<application
7+
<application
148
android:name=".MainApplication"
159
android:allowBackup="true"
16-
android:label="@string/app_name"
1710
android:icon="@mipmap/ic_launcher"
11+
android:label="@string/app_name"
1812
android:theme="@style/AppTheme">
19-
<activity
20-
android:name=".MainActivity"
21-
android:label="@string/app_name"
13+
14+
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity"/>
15+
16+
<activity
17+
android:name=".ActivityA"
2218
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
23-
android:windowSoftInputMode="adjustResize">
24-
<intent-filter>
25-
<action android:name="android.intent.action.MAIN" />
26-
<category android:name="android.intent.category.LAUNCHER" />
27-
</intent-filter>
28-
</activity>
29-
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
30-
</application>
31-
32-
</manifest>
19+
android:windowSoftInputMode="adjustResize"
20+
>
21+
<intent-filter>
22+
<action android:name="android.intent.action.MAIN"/>
23+
<category android:name="android.intent.category.LAUNCHER"/>
24+
</intent-filter>
25+
</activity>
26+
27+
<activity
28+
android:name=".ActivityD"
29+
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
30+
android:windowSoftInputMode="adjustResize"
31+
/>
32+
33+
<activity
34+
android:name=".reactnative.MyReactEntryActivity"
35+
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
36+
android:windowSoftInputMode="adjustResize"
37+
/>
38+
39+
</application>
40+
41+
</manifest>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.reactnativenavigationdemo;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.Nullable;
5+
import android.support.v7.app.AppCompatActivity;
6+
import android.view.View;
7+
import com.reactnativenavigationdemo.reactnative.MyReactEntryActivity;
8+
9+
public class ActivityA extends AppCompatActivity {
10+
11+
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
12+
super.onCreate(savedInstanceState);
13+
setContentView(R.layout.activity_a);
14+
}
15+
16+
public void jumpToRnHomeScreen(View v) {
17+
startActivity(MyReactEntryActivity.callHomeScreenIntent(this));
18+
}
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.reactnativenavigationdemo;
2+
3+
import android.app.Activity;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.os.Bundle;
7+
import android.support.annotation.Nullable;
8+
import android.support.v7.app.AppCompatActivity;
9+
import android.view.View;
10+
import com.reactnativenavigationdemo.reactnative.MyReactEntryActivity;
11+
12+
public class ActivityD extends AppCompatActivity {
13+
14+
public static Intent callIntent(Context context) {
15+
Intent intent = new Intent(context, ActivityD.class);
16+
if (!(context instanceof Activity)) {
17+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
18+
}
19+
return intent;
20+
}
21+
22+
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
23+
super.onCreate(savedInstanceState);
24+
setContentView(R.layout.activity_d);
25+
}
26+
27+
public void jumpToRnProfileScreen(View v) {
28+
startActivity(MyReactEntryActivity.callProfileScreenIntent(this, "I'm from native",
29+
"This is a message from native code"));
30+
}
31+
}

android/app/src/main/java/com/reactnativenavigationdemo/MainActivity.java

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,37 @@
11
package com.reactnativenavigationdemo;
22

33
import android.app.Application;
4-
54
import com.facebook.react.ReactApplication;
65
import com.facebook.react.ReactNativeHost;
76
import com.facebook.react.ReactPackage;
87
import com.facebook.react.shell.MainReactPackage;
98
import com.facebook.soloader.SoLoader;
10-
9+
import com.reactnativenavigationdemo.reactnative.packages.MyReactPackage;
1110
import java.util.Arrays;
1211
import java.util.List;
1312

1413
public class MainApplication extends Application implements ReactApplication {
1514

1615
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
17-
@Override
18-
public boolean getUseDeveloperSupport() {
16+
@Override public boolean getUseDeveloperSupport() {
1917
return BuildConfig.DEBUG;
2018
}
2119

22-
@Override
23-
protected List<ReactPackage> getPackages() {
24-
return Arrays.<ReactPackage>asList(
25-
new MainReactPackage()
26-
);
20+
@Override protected List<ReactPackage> getPackages() {
21+
return Arrays.<ReactPackage>asList(new MainReactPackage(), new MyReactPackage());
2722
}
2823

29-
@Override
30-
protected String getJSMainModuleName() {
24+
@Override protected String getJSMainModuleName() {
3125
return "index";
3226
}
3327
};
3428

35-
@Override
36-
public ReactNativeHost getReactNativeHost() {
29+
@Override public ReactNativeHost getReactNativeHost() {
3730
return mReactNativeHost;
3831
}
3932

40-
@Override
41-
public void onCreate() {
33+
@Override public void onCreate() {
4234
super.onCreate();
4335
SoLoader.init(this, /* native exopackage */ false);
4436
}
45-
}
37+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.reactnativenavigationdemo.reactnative;
2+
3+
import android.content.Context;
4+
import android.content.Intent;
5+
import android.os.Bundle;
6+
import android.text.TextUtils;
7+
import android.util.Log;
8+
import com.facebook.react.ReactInstanceManager;
9+
import com.facebook.react.bridge.ReactContext;
10+
import com.reactnativenavigationdemo.reactnative.base.AppCompatReactActivity;
11+
import com.reactnativenavigationdemo.reactnative.config.RNConfig;
12+
import com.reactnativenavigationdemo.reactnative.config.RNScreens;
13+
import javax.annotation.Nullable;
14+
15+
/**
16+
* RN入口类
17+
*/
18+
public class MyReactEntryActivity extends AppCompatReactActivity
19+
implements ReactInstanceManager.ReactInstanceEventListener {
20+
21+
private static final String TAG = MyReactEntryActivity.class.getSimpleName();
22+
23+
@Nullable @Override protected String getMainComponentName() {
24+
return RNConfig.RN_REGISTER_COMPONENT_KEY;
25+
}
26+
27+
@Override protected void onCreate(Bundle savedInstanceState) {
28+
super.onCreate(savedInstanceState);
29+
getReactInstanceManager().addReactInstanceEventListener(this);
30+
}
31+
32+
private static Intent callIntent(Context context) {
33+
return new Intent(context, MyReactEntryActivity.class);
34+
}
35+
36+
/**
37+
* 未遵循迪米特原则,这个入口并不知晓要跳转的 RN 界面需要什么业务数据
38+
*
39+
* @param bundle 给 RN 页面传递的数据随意包装
40+
*/
41+
public static Intent callIntent(Context context, Bundle bundle) {
42+
Intent intent = callIntent(context);
43+
intent.putExtras(bundle);
44+
return intent;
45+
}
46+
47+
public static Intent callHomeScreenIntent(Context context) {
48+
Intent intent = callIntent(context);
49+
intent.putExtra(RNConfig.SCREEN, RNScreens.HOME);
50+
return intent;
51+
}
52+
53+
public static Intent callProfileScreenIntent(Context context, String name, String msg) {
54+
Intent intent = callIntent(context);
55+
intent.putExtra(RNConfig.SCREEN, RNScreens.PROFILE);
56+
intent.putExtra(RNConfig.NAME, TextUtils.isEmpty(name) ? "" : name);
57+
intent.putExtra(RNConfig.MESSAGE, TextUtils.isEmpty(msg) ? "" : msg);
58+
return intent;
59+
}
60+
61+
@Nullable @Override protected Bundle getLaunchOptions() {
62+
if (getIntent() != null) {
63+
Bundle bundle = getIntent().getExtras();
64+
if (bundle == null) {
65+
bundle = new Bundle();
66+
}
67+
return bundle;
68+
} else {
69+
return new Bundle();
70+
}
71+
}
72+
73+
@Override public void onReactContextInitialized(ReactContext context) {
74+
Log.d(TAG, "onReactContextInitialized...");
75+
}
76+
}

0 commit comments

Comments
 (0)