Last active
April 3, 2018 08:52
-
-
Save githubdoramon/c40108c293ff370f96211a9adedf95cc to your computer and use it in GitHub Desktop.
Example on how to use React Native as fragments in Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public abstract class BaseReactableActivity extends BaseActivity { | |
| protected ReactInstanceManager mReactInstanceManager; | |
| private int OVERLAY_PERMISSION_REQ_CODE = 732; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && SystemConstants.IS_DEVELOPMENT) { | |
| if (!Settings.canDrawOverlays(this)) { | |
| Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, | |
| Uri.parse("package:" + getPackageName())); | |
| startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE); | |
| } | |
| } | |
| mReactInstanceManager = | |
| ((MyApplication) getApplication()).getReactNativeHost().getReactInstanceManager(); | |
| } | |
| @Override | |
| protected void onPause() { | |
| super.onPause(); | |
| if (mReactInstanceManager != null) { | |
| mReactInstanceManager.onHostPause(this); | |
| } | |
| } | |
| @Override | |
| protected void onResume() { | |
| super.onResume(); | |
| if (mReactInstanceManager != null) { | |
| mReactInstanceManager.onHostResume(this, this); | |
| } | |
| } | |
| @Override | |
| protected void onDestroy() { | |
| super.onDestroy(); | |
| if (mReactInstanceManager != null) { | |
| mReactInstanceManager.onHostDestroy(this); | |
| } | |
| } | |
| @Override | |
| public void onNewIntent(Intent intent) { | |
| super.onNewIntent(intent); | |
| mReactInstanceManager.onNewIntent(intent); | |
| } | |
| @Override | |
| protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
| super.onActivityResult(requestCode, resultCode, data); | |
| if (requestCode == OVERLAY_PERMISSION_REQ_CODE) { | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
| if (!Settings.canDrawOverlays(this)) { | |
| // SYSTEM_ALERT_WINDOW permission not granted... | |
| } | |
| } | |
| } | |
| mReactInstanceManager.onActivityResult(this, requestCode, resultCode, data); | |
| } | |
| @Override | |
| public boolean onKeyUp(int keyCode, KeyEvent event) { | |
| if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) { | |
| mReactInstanceManager.showDevOptionsDialog(); | |
| return true; | |
| } | |
| return super.onKeyUp(keyCode, event); | |
| } | |
| @Override | |
| public void onBackPressed() { | |
| if (mReactInstanceManager != null) { | |
| mReactInstanceManager.onBackPressed(); | |
| } else { | |
| super.onBackPressed(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ExampleActivity extends BaseReactableActivity { | |
| private SectionAdapter mSectionsPagerAdapter; | |
| private ViewPager mViewPager; | |
| TabLayout tabLayout; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| tabLayout = (TabLayout) findViewById(R.id.tabLayout); | |
| mSectionsPagerAdapter = new ProductsSectionAdapter(getSupportFragmentManager()); | |
| mViewPager = (ViewPager) findViewById(R.id.container); | |
| mViewPager.setAdapter(mSectionsPagerAdapter); | |
| tabLayout.setupWithViewPager(mViewPager); | |
| } | |
| public class SectionAdapter extends FragmentPagerAdapter { | |
| public ProductsSectionAdapter(FragmentManager fm) { | |
| super(fm); | |
| } | |
| @Override | |
| public Fragment getItem(int position) { | |
| switch (position) { | |
| case 0: | |
| return new ExampleFragment(); | |
| case 1: | |
| return new ExampleFragment2(); | |
| default: | |
| return null; | |
| } | |
| } | |
| @Override | |
| public int getCount() { | |
| return 2; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ExampleFragment extends ReactFragment { | |
| @Override | |
| public String getMainComponentName() { | |
| return "ExampleComponent"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public abstract class ReactFragment extends Fragment { | |
| private ReactRootView mReactRootView; | |
| private ReactInstanceManager mReactInstanceManager; | |
| // This method returns the name of our top-level component to show | |
| public abstract String getMainComponentName(); | |
| @Override | |
| public void onAttach(Context context) { | |
| super.onAttach(context); | |
| mReactRootView = new ReactRootView(context); | |
| mReactInstanceManager = | |
| ((MusilinxApplication) getActivity().getApplication()) | |
| .getReactNativeHost() | |
| .getReactInstanceManager(); | |
| } | |
| @Override | |
| public ReactRootView onCreateView(LayoutInflater inflater, ViewGroup group, Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| return mReactRootView; | |
| } | |
| @Override | |
| public void onActivityCreated(Bundle savedInstanceState) { | |
| super.onActivityCreated(savedInstanceState); | |
| mReactRootView.startReactApplication( | |
| mReactInstanceManager, | |
| getMainComponentName(), | |
| null | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment