Convert to using new React Native MainActivity template

Adjusted the README to reflect the modern way of doing things.
This commit is contained in:
Scott Kyle 2016-02-12 11:00:46 -08:00
parent 6e4b2440ba
commit e594087644
3 changed files with 56 additions and 133 deletions

View File

@ -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` - Run this command from the project directory: `react-native link realm`
- Open `MainActivity.java` inside your project: - Open `MainActivity.java` inside your project:
- Add `import io.realm.react.RealmReactPackage;` under the other imports. - 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! - You can now `require('realm')` in your Android app's JS to use Realm!
## Getting Started ## Getting Started

View File

@ -1,81 +1,43 @@
package io.realm.react.example; package io.realm.react.example;
import android.app.Activity; import com.facebook.react.ReactActivity;
import android.os.Bundle; import com.facebook.react.ReactPackage;
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.shell.MainReactPackage; import com.facebook.react.shell.MainReactPackage;
import java.util.Arrays;
import java.util.List;
import io.realm.react.RealmReactPackage; import io.realm.react.RealmReactPackage;
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler { public class MainActivity extends ReactActivity {
private ReactInstanceManager mReactInstanceManager;
private ReactRootView mReactRootView;
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected String getMainComponentName() {
super.onCreate(savedInstanceState); return "ReactExample";
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);
} }
/**
* Returns whether dev mode should be enabled.
* This enables e.g. the dev menu.
*/
@Override @Override
public boolean onKeyUp(int keyCode, KeyEvent event) { protected boolean getUseDeveloperSupport() {
if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) { return BuildConfig.DEBUG;
mReactInstanceManager.showDevOptionsDialog();
return true;
}
return super.onKeyUp(keyCode, event);
} }
/**
* 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 @Override
public void onBackPressed() { protected List<ReactPackage> getPackages() {
if (mReactInstanceManager != null) { return Arrays.<ReactPackage>asList(
mReactInstanceManager.onBackPressed(); new MainReactPackage(),
} else { new RealmReactPackage()
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);
}
} }
} }

View File

@ -1,84 +1,45 @@
package io.realm.react.testapp; package io.realm.react.testapp;
import android.app.Activity; import com.facebook.react.ReactActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import com.facebook.react.LifecycleState;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactPackage; 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.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; import io.realm.react.RealmReactPackage;
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler { public class MainActivity extends ReactActivity {
private ReactInstanceManager mReactInstanceManager;
private ReactRootView mReactRootView;
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected String getMainComponentName() {
super.onCreate(savedInstanceState); return "ReactTests";
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);
} }
/**
* Returns whether dev mode should be enabled.
* This enables e.g. the dev menu.
*/
@Override @Override
public boolean onKeyUp(int keyCode, KeyEvent event) { protected boolean getUseDeveloperSupport() {
if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) { return BuildConfig.DEBUG;
mReactInstanceManager.showDevOptionsDialog();
return true;
}
return super.onKeyUp(keyCode, event);
} }
/**
* 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 @Override
public void onBackPressed() { protected List<ReactPackage> getPackages() {
if (mReactInstanceManager != null) { return Arrays.<ReactPackage>asList(
mReactInstanceManager.onBackPressed(); new MainReactPackage(),
} else { new RNFSPackage(),
super.onBackPressed(); new RealmReactPackage()
} );
}
@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);
}
} }
} }