title: Accessing AppCompatActivity in custom native module
body: <p>Currently building a custom native module using Braintree Android SDK v3 for use in react-native. The SDK initialization required a parameter of type AppCompatActivity or Fragment. I'm having trouble finding a way to pass that parameter as there doesn't seem to exist a method that returns the correct type.</p>
  
  <p>The compile error:</p>
  
  <pre class="lang-sh prettyprint-override"><code>this.mBraintreeFragment = BraintreeFragment.newInstance(getCurrentActivity(), token);
                                                   ^
  method BraintreeFragment.newInstance(AppCompatActivity,String) is not applicable
        (argument mismatch; Activity cannot be converted to AppCompatActivity)
  
  method BraintreeFragment.newInstance(Fragment,String) is not applicable
        (argument mismatch; Activity cannot be converted to Fragment)
  </code></pre>
  
  <p>I've tried to replace </p>
  
  <pre class="lang-java prettyprint-override"><code>public class MainApplication extends Application implements ReactApplication {
  </code></pre>
  
  <p>with </p>
  
  <pre class="lang-java prettyprint-override"><code>public class MainApplication extends AppCompatActivity implements ReactApplication {
  </code></pre>
  
  <p>in MainApplication.java, but that doesn't seem to solve the problem, and leads to some other errors.</p>
  
  <p>Hoping someone could assists.</p>
  
  <p><strong>Edit: Clarification regarding project setup.</strong></p>
  
  <p>React-Native v0.60.5</p>
  
  <p>Files:</p>
  
  <pre><code>java/com/my.app/MainActivity.java
  java/com/my.app/MainApplication.java
  java/com/my.app/braintree/BraintreePackage.java
  java/com/my.app/braintree/BraintreeModule.java
  </code></pre>
  
  <p>MainAcitivity.java</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.my.app;
  
  // Default
  import com.facebook.react.ReactActivity;
  
  // Custom added
  import android.content.pm.PackageInfo;
  import android.content.pm.PackageManager;
  import android.os.Bundle;
  import com.facebook.react.ReactActivityDelegate;
  
  
  
  public class MainActivity extends ReactActivity {
  
      /**
       * Returns the name of the main component registered from JavaScript.
       * This is used to schedule rendering of the component.
       */
      @Override
      protected String getMainComponentName() {
          return "appname";
      }
  
      /**
       * Injects build number as prop to root app component in react
       */
      @Override
      protected ReactActivityDelegate createReactActivityDelegate() {
          return new ReactActivityDelegate(this, getMainComponentName()) {
              @Override
              protected Bundle getLaunchOptions() {
                  Bundle initialProps = getMyLaunchOptions();
                  return initialProps;
              }
          };
      }
      protected Bundle getMyLaunchOptions() {
          Bundle bundle = new Bundle();
          PackageInfo pInfo = null;
          try {
              pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
              int build = pInfo.versionCode;
              bundle.putString("build", String.valueOf(build));
          } catch (PackageManager.NameNotFoundException e) {
              e.printStackTrace();
          }
          return bundle;
      }
  }
  
  </code></pre>
  
  <p>MainApplication.java</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.my.app;
  
  import android.app.Application;
  import android.util.Log;
  
  import com.facebook.react.PackageList;
  import com.facebook.hermes.reactexecutor.HermesExecutorFactory;
  import com.facebook.react.bridge.JavaScriptExecutorFactory;
  import com.facebook.react.ReactApplication;
  import com.facebook.react.ReactNativeHost;
  import com.facebook.react.ReactPackage;
  import com.facebook.soloader.SoLoader;
  
  import java.util.List;
  
  // Native Braintree
  import com.my.app.braintree.BraintreePackage;
  
  
  public class MainApplication extends Application implements ReactApplication {
  
    private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
      @Override
      public boolean getUseDeveloperSupport() {
        return BuildConfig.DEBUG;
      }
  
      @Override
      protected List&lt;ReactPackage&gt; getPackages() {
        @SuppressWarnings("UnnecessaryLocalVariable")
        List&lt;ReactPackage&gt; packages = new PackageList(this).getPackages();
        // Packages that cannot be autolinked yet can be added manually here, for example:
        // packages.add(new MyReactNativePackage());
  
        // Custom Native Braintree
        packages.add(new BraintreePackage());
  
        return packages;
      }
  
      @Override
      protected String getJSMainModuleName() {
        return "index";
      }
    };
  
    @Override
    public ReactNativeHost getReactNativeHost() {
      return mReactNativeHost;
    }
  
    @Override
    public void onCreate() {
      super.onCreate();
      SoLoader.init(this, /* native exopackage */ false);
    }
  }
  
  </code></pre>
  
  <p>BraintreePackage.java</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.my.app.braintree;
  
  import com.facebook.react.ReactPackage;
  import com.facebook.react.bridge.NativeModule;
  import com.facebook.react.bridge.ReactApplicationContext;
  import com.facebook.react.uimanager.ViewManager;
  
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.List;
  
  public class BraintreePackage implements ReactPackage {
  
    @Override
    public List&lt;ViewManager&gt; createViewManagers(ReactApplicationContext reactContext) {
      return Collections.emptyList();
    }
  
    @Override
    public List&lt;NativeModule&gt; createNativeModules(ReactApplicationContext reactContext) {
      List&lt;NativeModule&gt; modules = new ArrayList&lt;&gt;();
      modules.add(new BraintreeModule(reactContext));
      return modules;
    }
  }
  </code></pre>
  
  <p>BraintreeModule.java</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.my.app.braintree;
  
  
  import com.facebook.react.bridge.ReactApplicationContext;
  import com.facebook.react.bridge.ReactContextBaseJavaModule;
  import com.facebook.react.bridge.ReactMethod;
  
  import com.facebook.react.bridge.Callback;
  
  import android.content.Context;
  
  // Braintree
  import com.braintreepayments.api.*;
  import com.braintreepayments.api.exceptions.*;
  
  public class BraintreeModule extends ReactContextBaseJavaModule {
  
    private final ReactApplicationContext reactContext;
  
    private String token;
    private Context mActivityContext;
    private BraintreeFragment mBraintreeFragment;
  
    public BraintreeModule(ReactApplicationContext reactContext) {
      super(reactContext);
      this.reactContext = reactContext;
    }
  
    @Override
    public String getName() {
      return "BraintreeModule";
    }
  
    public String getToken() {
      return this.token;
    }
  
    public void setToken(String token) {
      this.token = token;
    }
  
    // Initializes instance with client token
    @ReactMethod
    public void setup(
      final String clientToken,
      final Callback successCallback,
      final Callback errorCallback
    ) {
      try {
        this.mBraintreeFragment = BraintreeFragment.newInstance(getCurrentActivity(), clientToken);
        // Set token
        this.setToken(clientToken);
        successCallback.invoke(this.getToken());
      } catch (InvalidArgumentException e) {
        errorCallback.invoke(e.getMessage());
      }
    }
  
  }
  </code></pre>