From e59408764482fe2030fc7dc264dc9549cbb724db Mon Sep 17 00:00:00 2001 From: Scott Kyle Date: Fri, 12 Feb 2016 11:00:46 -0800 Subject: [PATCH] Convert to using new React Native MainActivity template Adjusted the README to reflect the modern way of doing things. --- README.md | 2 +- .../io/realm/react/example/MainActivity.java | 92 ++++++------------ .../app/src/main/java/MainActivity.java | 95 ++++++------------- 3 files changed, 56 insertions(+), 133 deletions(-) diff --git a/README.md b/README.md index 951eeedb..959c2601 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ The ReactNative example project is in the `examples/ReactExample` directory. You - Run this command from the project directory: `react-native link realm` - Open `MainActivity.java` inside your project: - Add `import io.realm.react.RealmReactPackage;` under the other imports. - - Add `.addPackage(new RealmReactPackage())` directly under `.addPackage(new MainReactPackage())` + - Add `new RealmReactPackage()` to the list returned by the `getPackages()` method. - You can now `require('realm')` in your Android app's JS to use Realm! ## Getting Started diff --git a/examples/ReactExample/android/app/src/main/java/io/realm/react/example/MainActivity.java b/examples/ReactExample/android/app/src/main/java/io/realm/react/example/MainActivity.java index 59b09e5b..c4631b25 100644 --- a/examples/ReactExample/android/app/src/main/java/io/realm/react/example/MainActivity.java +++ b/examples/ReactExample/android/app/src/main/java/io/realm/react/example/MainActivity.java @@ -1,81 +1,43 @@ package io.realm.react.example; -import android.app.Activity; -import android.os.Bundle; -import android.view.KeyEvent; - -import com.facebook.react.LifecycleState; -import com.facebook.react.ReactInstanceManager; -import com.facebook.react.ReactRootView; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler; +import com.facebook.react.ReactActivity; +import com.facebook.react.ReactPackage; import com.facebook.react.shell.MainReactPackage; +import java.util.Arrays; +import java.util.List; + import io.realm.react.RealmReactPackage; -public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler { - - private ReactInstanceManager mReactInstanceManager; - private ReactRootView mReactRootView; +public class MainActivity extends ReactActivity { + /** + * Returns the name of the main component registered from JavaScript. + * This is used to schedule rendering of the component. + */ @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - mReactRootView = new ReactRootView(this); - - mReactInstanceManager = ReactInstanceManager.builder() - .setApplication(getApplication()) - .setBundleAssetName("index.android.bundle") - .setJSMainModuleName("index.android") - .addPackage(new MainReactPackage()) - .addPackage(new RealmReactPackage()) - .setUseDeveloperSupport(BuildConfig.DEBUG) - .setInitialLifecycleState(LifecycleState.RESUMED) - .build(); - - mReactRootView.startReactApplication(mReactInstanceManager, "ReactExample", null); - - setContentView(mReactRootView); + protected String getMainComponentName() { + return "ReactExample"; } + /** + * Returns whether dev mode should be enabled. + * This enables e.g. the dev menu. + */ @Override - public boolean onKeyUp(int keyCode, KeyEvent event) { - if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) { - mReactInstanceManager.showDevOptionsDialog(); - return true; - } - return super.onKeyUp(keyCode, event); + protected boolean getUseDeveloperSupport() { + return BuildConfig.DEBUG; } + /** + * A list of packages used by the app. If the app uses additional views + * or modules besides the default ones, add more packages here. + */ @Override - public void onBackPressed() { - if (mReactInstanceManager != null) { - mReactInstanceManager.onBackPressed(); - } else { - super.onBackPressed(); - } - } - - @Override - public void invokeDefaultOnBackPressed() { - super.onBackPressed(); - } - - @Override - protected void onPause() { - super.onPause(); - - if (mReactInstanceManager != null) { - mReactInstanceManager.onPause(); - } - } - - @Override - protected void onResume() { - super.onResume(); - - if (mReactInstanceManager != null) { - mReactInstanceManager.onResume(this, this); - } + protected List getPackages() { + return Arrays.asList( + new MainReactPackage(), + new RealmReactPackage() + ); } } diff --git a/tests/react-test-app/android/app/src/main/java/MainActivity.java b/tests/react-test-app/android/app/src/main/java/MainActivity.java index 3b840dd6..6610f65d 100644 --- a/tests/react-test-app/android/app/src/main/java/MainActivity.java +++ b/tests/react-test-app/android/app/src/main/java/MainActivity.java @@ -1,84 +1,45 @@ package io.realm.react.testapp; -import android.app.Activity; -import android.os.Bundle; -import android.view.KeyEvent; - -import com.facebook.react.LifecycleState; -import com.facebook.react.ReactInstanceManager; +import com.facebook.react.ReactActivity; import com.facebook.react.ReactPackage; -import com.facebook.react.ReactRootView; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler; import com.facebook.react.shell.MainReactPackage; -import com.rnfs.RNFSPackage; +import java.util.Arrays; +import java.util.List; + +import com.rnfs.RNFSPackage; import io.realm.react.RealmReactPackage; -public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler { - - private ReactInstanceManager mReactInstanceManager; - private ReactRootView mReactRootView; +public class MainActivity extends ReactActivity { + /** + * Returns the name of the main component registered from JavaScript. + * This is used to schedule rendering of the component. + */ @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - mReactRootView = new ReactRootView(this); - - mReactInstanceManager = ReactInstanceManager.builder() - .setApplication(getApplication()) - .setBundleAssetName("index.android.bundle") - .setJSMainModuleName("index.android") - .addPackage(new MainReactPackage()) - .addPackage(new RNFSPackage()) - .addPackage(new RealmReactPackage()) - .setUseDeveloperSupport(BuildConfig.DEBUG) - .setInitialLifecycleState(LifecycleState.RESUMED) - .build(); - - mReactRootView.startReactApplication(mReactInstanceManager, "ReactTests", null); - - setContentView(mReactRootView); + protected String getMainComponentName() { + return "ReactTests"; } + /** + * Returns whether dev mode should be enabled. + * This enables e.g. the dev menu. + */ @Override - public boolean onKeyUp(int keyCode, KeyEvent event) { - if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) { - mReactInstanceManager.showDevOptionsDialog(); - return true; - } - return super.onKeyUp(keyCode, event); + protected boolean getUseDeveloperSupport() { + return BuildConfig.DEBUG; } + /** + * A list of packages used by the app. If the app uses additional views + * or modules besides the default ones, add more packages here. + */ @Override - public void onBackPressed() { - if (mReactInstanceManager != null) { - mReactInstanceManager.onBackPressed(); - } else { - super.onBackPressed(); - } - } - - @Override - public void invokeDefaultOnBackPressed() { - super.onBackPressed(); - } - - @Override - protected void onPause() { - super.onPause(); - - if (mReactInstanceManager != null) { - mReactInstanceManager.onPause(); - } - } - - @Override - protected void onResume() { - super.onResume(); - - if (mReactInstanceManager != null) { - mReactInstanceManager.onResume(this, this); - } + protected List getPackages() { + return Arrays.asList( + new MainReactPackage(), + new RNFSPackage(), + new RealmReactPackage() + ); } }