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;
  }
}
4
  • 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. Commented Jan 29, 2017 at 22:33
  • I'm using an external library which was structured to be used that way... Commented Jan 29, 2017 at 22:45
  • No you aren't. There's no reason that the ReactApplication class needs to be the actual Application class. Commented Jan 29, 2017 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.. Commented Jan 29, 2017 at 22:57

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@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 You need to override all methods. In the answer I didnt for readability reasons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.