React-native v0.29.x after the Android platform part plug-in cannot use the problem

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.