React Native modules into an existing app - android

I've been working on adding react native to an existing app. I followed the guide on the official docs and after fixing some errors that popped up all was well.
Now I want to use react native components to talk to native code. There are a few use-cases that I need but the current one is that I have an AndroidToolbar and when clicking the navIcon I want to end the current Activity (I want to use native navigation). My thought was to create a native module and just simply call it from my react native code (natively calling onBackPressed). The problem is that when following the docs they suggest to add the package to getPackages function within your ReactApplication. Something like:
public class MainApplication extends Application implements ReactApplication {
...
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new AnExampleReactPackage());
}
...
}
The problem is that if you follow the "Integration with existing apps" docs then you never create a ReactApplication. Instead you just create an Activity somewhere in your app and just start that activity somewhere in your app. If anyone can point me in the right direction for how to call native code when you have integrated into an existing app, that would be greatly appreciated.

Related

React native draggable flatlist not draggable

I tried implementing react-native-draggable-flatlist to my project, but I'm not able to drag the elements. I am able to "pick them" (They get bigger if I press them), but I can't move them to another position.
I had this problem before and found a working soloution in a GitHub issue, but as it seems, MainActivity.java has changed in some version and I don't know how to correctly upgrade it.
The soloution I found:
When I add the #Override for the ReactActivityDelegate, my App crashes after the build.
I noticed that my MainActivity.java looks a little bit different, it seems like the ActivityDelegate is Managed through a class called MainActivityDelegate which extends ReactActivityDelegate:
public static class MainActivityDelegate extends ReactActivityDelegate {
public MainActivityDelegate(ReactActivity activity, String mainComponentName) {
super(activity, mainComponentName);
}
#Override
protected ReactRootView createRootView() {
ReactRootView reactRootView = new ReactRootView(getContext());
// If you opted-in for the New Architecture, we enable the Fabric Renderer.
reactRootView.setIsFabric(BuildConfig.IS_NEW_ARCHITECTURE_ENABLED);
return reactRootView;
}
}
It seems like I have to change something here to achieve the same result, but I don't really know what.
EDIT:
According to the RNGH docs you shouldn't change your MainActivity.java that way anymore and wrap your whole App into a GestureHandlerRootView. Some Libaries like react-native-navigation do this by default (in my case). BEcause of that (I think) my App gets completely grey when I use a GestureHandlerRootView. If I don't addd it myself, so I use the one from neavigaion, I still can't drag the items in my list.
I fixed it by wrapping my screen component in a GestureHandlerRootView

Trouble instantiating gdx-pay purchaseManager in my game

I am trying to set up gdx-pay in my game. I have followed the official readme (https://github.com/libgdx/gdx-pay/), but can't seem to instantiate the PurchaseManager in my AndroidLauncher.
I have set up a class that handles PurchaseManager installation, but every time I try to instantiate it in the AndroidLauncher I get this error:
"java.lang.RuntimeException: Unable to start activity ComponentInfo{appPackage.AndroidLauncher}: java.lang.IllegalArgumentException: Support for pending purchases must be enabled. Enable this by calling 'enablePendingPurchases()' on BillingClientBuilder."
The readme says to add this to the AndroidLauncher onCreate
game.purchaseManager = new PurchaseManagerGoogleBilling(this);
The problem is that I have to add this
ProjectName.purchaseManager = new PurchaseManagerGoogleBilling(this);
This give me an error saying that I have to make purchaseManager static in my ProjectName class
When I do this, I get the RuntimeException mentioned above.
The sample gdx-pay project instantiates PurchaseManager with this code
public class AndroidLauncher extends GenericAndroidLauncher {
#Override
protected void initFlavor(GdxPayApp game) {
super.initFlavor(game);
game.purchaseManager = new PurchaseManagerGoogleBilling(this);
}
}
When I try this, #Override is invalid and initFlavor does not exist. I tried looking for initFlavor in the gdx-pay files, but had no luck finding anything...
Thanks for taking the time to help
Well it turns out that the following line in my android gradle was the issue
implementation 'com.android.billingclient:billing:2.0.1'
Gdx-pay takes care of this automatically and uses client 1.1
Setting the billingclient to 2.0.1 was the problem. I was able to fix it by deleting that line. Big thanks to the libGdx discord guys!

Using UserDialogs in Android

I'm stucked with usage of plugin Acr.UserDialogs in android app, based on MVVMCross.
In PCL project i used IUserDialog in viewmodel constructor injection.
I have installed Acr.UserDialogs package both in PCL and in Droid project, but when i run app, it throws:
In android, you must call UserDialogs.Init(Activity) from your first
activity OR UserDialogs.Init(App) from your custom application OR
provide a factory function to get the current top activity via
UserDialogs.Init(() => supply top activity)
I tryed to call in my viewModel:
UserDialogs.Init(this);
But Init is not recognized
And calling of UserDialogs.Instance.Loading ().Hide(); in app throws the same issue.
How it should be initialized in android project?
Upd: Final solution to workaround this looks like:
In PCL project App.cs add: Mvx.RegisterSingleton(() =>
UserDialogs.Instance);
In Your first loaded activity in OnCreate
add: UserDialogs.Init(() => this);
This error is very clearly. You can't initialize it in viewModel, You can only do that in your main activity.
FAQ
I'm getting a nullreferenceexception when using loading.
This happens when you run loading (or almost any dialog) from the
constructor of your page or viewmodel. The view hasn't been rendered
yet, therefore there is nothing to render to.
Android Initialization In your MainActivity
UserDialogs.Init(this);
OR UserDialogs.Init(() => provide your own top level activity provider)
OR MvvmCross - UserDialogs.Init(() => Mvx.Resolve<IMvxTopActivity>().Activity)
OR Xamarin.Forms - UserDialogs.Init(() => (Activity)Forms.Context)
GitHub docs.

Create and change to new view with React Native

We're currently working on an app for one of our customers, and we've decided to use React Native as the technology to use for creating the app.
I'm pretty new to this technology, and I'm used to developing apps with either C# or Java, where I have full-fledged IDEs that provide all the functionality I need.
I'm currently creating a login screen, and would like to change to different views, depending on the type of login (e.g.: Facebook, Google, Twitter, Email, etc.).
How do I go about doing this. As I mention, with an IDE, it'd be a shortcut like SHIFT+ALT+A or something to create a new item, select type, name it and the IDE does the rest.
Do I have to create a new xx.android/ios.js file, and somehow call that, or do I need to go in to the native backend of the different projects?
What I tried was something along the lines of:
class Class extends Component {
render() {
<UI code here>
<Button onPress={this.eventHandler}
}
eventHandler(event) {
var xxLogin = new xxLogin();
xxLogin.render();
}
}
class xxLogin extends Component {
render() {
<UI code here>
}
}
But that failed, which I expected.
Thanks in advance for any help!
P.S.: I'm using Mac OS X El Capitan, if that helps.

Android + Phonegap + Admob

As the title suggests I am trying to create an Android app using Phonegap and then trying to insert Admob into it.
I am using this tutorial provided by Adobe and it is working fine. After this when I use this tutorial provided by Google I run into problems.
According to the Google tutorial, I have to change the activity class, below is the original and the second block of code is the altered code. There doesn't seem to be any errors preventing it from compiling. It's just when I try and run it using the Android emulator I get an error stating "Unfortunately MyFirstPhonegapPlugin has stopped"
Note, using the Google tutorial, there are two ways of implementing the code, using just the class below and using a mixture of XML and the class below. I tried the other way and due to getting errors which doesn't let me compile, I've gone for this way.
package com.tricedesigns;
import org.apache.cordova.DroidGap;
import android.os.Bundle;
public class MyFirstPhoneGapPluginActivity extends DroidGap {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
}
Adjusted code:
package com.tricedesigns;
import org.apache.cordova.DroidGap;
import android.os.Bundle;
import com.google.ads.*;
public class MyFirstPhoneGapPluginActivity extends DroidGap {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
// Look up the AdView as a resource and load a request.
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
}
}
I am using Phonegap 1.6.1 and the most up to date SDK for Android and Admob
The way Phonegap is implemented, it pretty much hides all the details about Android and lets you basically implement a mobile web app as a native app. If you care to implement Android natively, I'd recommend reading the Hello World tutorial. The code you have here is crashing on AdView adView = (AdView)this.findViewById(R.id.adView); because you don't have a layout file with an AdView element with id adView.
If you're more interested in the mobile web part of it and plan to continue sticking with Phonegap, your best bet is to try this plugin which lets you make JavaScript calls using the Phonegap framework, and the plugin performs all of the underlying Android calls necessary to get ads.
Here is a fully functional project of what I was looking for
old version phonegap not support extenal jar lib.
but now phonegap add some new feathers.
use phonegap builder ,add config
add some config
<gap:plugin name="com.admob.plugin" version="1.0.0" source="plugins.cordova.io"/>
so not need any java code any more.
just need some js code.
admob.initAdmob("admob banner ID","admob interstitial ID");
admob.showBanner(admob.BannerSize.BANNER,admob.Position.TOP_APP);
this will create and show a admob banner at top of your app.

Categories

Resources