This article is for children's shoes that already have experience with the RN encapsulation Android native module. If you have any questions, please check the official documentation here
In the old version of the registration module, if the module has Activity parameters in the constructor , we can add this directly in parentheses ,
starting with v0.29.0 , theAndroid Project has added mainapplication.java templates, most of which were previously Mainactivity.java 's logic moved here.
Register module (in Mainapplication.java)
Import Ca.jaysoo.extradimensions.ExtraDimensionsPackage; <---Import
public class MainApplication extends application implements Reactapplication {
......
Protected list<reactpackage> getpackages () {
Return Arrays.<reactpackage>aslist (
New Mainreactpackage (),
New Extradimensionspackage ()//<---Add here
);
}
......
}
The above is just the code location migration, only new when The This parameter is missing, because this time this is no longer the current Activity , which is the main cause of the whole problem!
Because the current activity is no longer passed through the constructor, we can only try to retrieve the current activityin other ways.
So where is it? Friends who are familiar with native Android should know that there is a context getcurrentactivity () method that we want to use Activity Can be obtained by this method.
So the answer is now, and there's no mistake in getting the Activitywhen we create the module.
Before the 0.29 version, the constructor of this Orientationmodule would normally add the Activity parameter directly in parentheses ,
Public Orientationmodule (Reactapplicationcontext reactcontext,activity Activity) {
Super (Reactcontext);
}
And then add this at the time of registration , But the new version does not, so we are here to the JS method inside if you need to use the Activity of the place
Direct Adoption getcurrentyactivity () method on the line,
It's done here, and the rest is the basic configuration, not to repeat
React-native v0.29.x after the Android platform part plug-in cannot use the problem