2

How do you get and inject an activity in MainApplication? I have an external library that requires me to pass an activity as an argument. This answer suggests i use getCurrentActivity() method in the part that requires the current activity, but it has been removed from ReactContextBaseJavaModule class. Is there any other way to do this? How do I pass an activity where the 'this' keyword was used in the code below? Thanks..

package com.bluetoothioexample;

import android.app.Application;
import android.util.Log;

import com.facebook.react.bridge.ReactContextBaseJavaModule;

import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;

import java.util.Arrays;
import java.util.List;

import com.subsite.bluetoothio.BluetoothIOPackage;
import com.oblador.vectoricons.VectorIconsPackage;

public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    protected boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
          new BluetoothIOPackage(this), //<- How pass the activity
                                        // from ReactApplication ?
          new VectorIconsPackage()
      );
    }
  };

  @Override
  public ReactNativeHost getReactNativeHost() {
      return mReactNativeHost;
  }
}
Community
  • 1
  • 1
Badmus Taofeeq
  • 761
  • 9
  • 18
  • If you need this, you've misarchitected your code. Nothing with Activity scope or smaller should be known to the Application class. The Application class should only know about things that exist for the scope of the Application. – Gabe Sechan Jan 29 '17 at 22:33
  • I'm using an external library which was structured to be used that way... – Badmus Taofeeq Jan 29 '17 at 22:45
  • No you aren't. There's no reason that the ReactApplication class needs to be the actual Application class. – Gabe Sechan Jan 29 '17 at 22:46
  • Well, I'm following the structure provided by react-native's documentation. How would you do it? .. would be glad if you can provide a little bit of how you'd go implementing this as an answer to this question. Thanks.. – Badmus Taofeeq Jan 29 '17 at 22:57

1 Answers1

7

I am not into React-Native but there is simple trick

 public class MainApplication extends Application implements Application.ActivityLifecycleCallbacks{


    @Override
    public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(this);
    }


    private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
        @Override
        protected boolean getUseDeveloperSupport() {
            return BuildConfig.DEBUG;
        }

        @Override
        protected List<ReactPackage> getPackages() {
            return Arrays.<ReactPackage>asList(
                    new MainReactPackage(),
                    new BluetoothIOPackage(mCurrentActivity), //
                    // from ReactApplication ?
                    new VectorIconsPackage()
            );
        }
    };
    private Activity mCurrentActivity;

    @Override
    public ReactNativeHost getReactNativeHost() {
        return mReactNativeHost;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        mCurrentActivity = activity;
    }

You need to implement Application.ActivityLifecycleCallbacks and register lifecycle callback registerActivityLifecycleCallbacks(this); and wait for activity to be created to get the current reference.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • @Nikola: I tried the same, but got an error. Any suggestions. `MainApplication is not abstract and does not override abstract method onActivityDestroyed(Activity) in ActivityLifecycleCallbacks public class MainApplication extends Application implements Application.ActivityLifecycleCallbacks` – Mohit Pandey May 07 '17 at 16:58
  • 1
    @Mohit Pandey You need to override all methods. In the answer I didnt for readability reasons. – Nikola Despotoski May 07 '17 at 16:59