Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating libraries dependent on the activity in RN 0.29 #8509

Closed
avishayil opened this issue Jun 30, 2016 · 4 comments
Closed

Migrating libraries dependent on the activity in RN 0.29 #8509

avishayil opened this issue Jun 30, 2016 · 4 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@avishayil
Copy link

Hi,

Using RN 0.29 rc, I noticed that packages requiring activity as a parameter cannot be passed on MainApplication.java.

For example:
The package remobile/react-native-splash-screen

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
        new MainReactPackage(),
        new RCTSplashScreenPackage(this)
      );
    }

(this) gets a red line in Android Studio.

Did anyone found an appropriate solution?

@nihgwu
Copy link
Contributor

nihgwu commented Jun 30, 2016

49f20f4
please check this and I believe you will find the answer 😊 I'll paste my code if it's needed

@ryankask
Copy link
Contributor

@nihgwu That would be really helpful!

I wanted to update to 0.29 but wasn't sure how to handle react-native-fbsdk which requires implementing onCreate, onResume, onPause, and onActivityResult and uses a CallbackManager instance variable currently on ReactActivity

@nihgwu
Copy link
Contributor

nihgwu commented Jun 30, 2016

@ryankask well, the following is the working version for RN0.29 modified from react-native-splashscreen

package com.liteneo.reader;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactMethod;


public class SplashScreenModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
    private static Dialog splashDialog;
    private ImageView splashImageView;
    private boolean splashed = false;

    private Activity activity;

    public SplashScreenModule(ReactApplicationContext reactContext) {
        super(reactContext);
        reactContext.addLifecycleEventListener(this);
    }

    @Override
    public String getName() {
        return "SplashScreen";
    }

    @Override
    public void onHostResume() {
        if(splashed) return;
        splashed = true;
        show();
    }

    @Override
    public void onHostPause() {
    }

    @Override
    public void onHostDestroy() {
    }

    @ReactMethod
    public void hide() {
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                removeSplashScreen();
            }
        }, 500);
    }

    private void removeSplashScreen() {
        Activity currentActivity = getCurrentActivity();
        currentActivity.runOnUiThread(new Runnable() {
            public void run() {
                if (splashDialog != null && splashDialog.isShowing()) {
                    AlphaAnimation fadeOut = new AlphaAnimation(1, 0);
                    fadeOut.setDuration(1000);
                    View view = ((ViewGroup)splashDialog.getWindow().getDecorView()).getChildAt(0);
                    view.startAnimation(fadeOut);
                    fadeOut.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                        }
                        @Override
                        public void onAnimationEnd(Animation animation) {
                            splashDialog.dismiss();
                            splashDialog = null;
                            splashImageView = null;
                        }
                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }
                    });
                }
            }
        });
    }

    private int getSplashId() {
        Activity currentActivity = getCurrentActivity();
        int drawableId = currentActivity.getResources().getIdentifier("screen", "drawable", currentActivity.getClass().getPackage().getName());
        if (drawableId == 0) {
            drawableId = currentActivity.getResources().getIdentifier("screen", "drawable", currentActivity.getPackageName());
        }
        return drawableId;
    }

    @ReactMethod
    public void show() {
        final int drawableId = getSplashId();
        if ((splashDialog != null && splashDialog.isShowing())||(drawableId == 0)) {
            return;
        }
        final Activity currentActivity = getCurrentActivity();
        currentActivity.runOnUiThread(new Runnable() {
            public void run() {
                // Get reference to display
                Display display = currentActivity.getWindowManager().getDefaultDisplay();
                Context context = currentActivity;

                // Use an ImageView to render the image because of its flexible scaling options.
                splashImageView = new ImageView(context);
                splashImageView.setImageResource(drawableId);
                LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                splashImageView.setLayoutParams(layoutParams);
                splashImageView.setMinimumHeight(display.getHeight());
                splashImageView.setMinimumWidth(display.getWidth());
                splashImageView.setBackgroundColor(Color.BLACK);
                splashImageView.setScaleType(ImageView.ScaleType.FIT_XY);

                // Create and show the dialog
                splashDialog = new Dialog(context, android.R.style.Theme_Translucent_NoTitleBar);
                // check to see if the splash screen should be full screen
                if ((currentActivity.getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN)
                        == WindowManager.LayoutParams.FLAG_FULLSCREEN) {
                    splashDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);
                }
                splashDialog.setContentView(splashImageView);
                splashDialog.setCancelable(false);
                splashDialog.show();
            }
        });
    }
}

@satya164
Copy link
Contributor

I'm going to close this as we've outlined the solutions in 49f20f4. The libraries need to be updated to use getCurrentActivity instead of passing a reference.

nihgwu referenced this issue Jul 1, 2016
Reviewed By: astreet

Differential Revision: D3328342

fbshipit-source-id: af4e825d0b7c2d3d4490094a939e97cc527dd242
@facebook facebook locked as resolved and limited conversation to collaborators Jun 30, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

5 participants