Skip to content

Commit

Permalink
Fix for #26
Browse files Browse the repository at this point in the history
  • Loading branch information
mehcode committed Jan 21, 2017
1 parent 3cdbb18 commit f8bfc54
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,59 @@

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;

import com.facebook.react.ReactActivity;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.ReactContext;

import java.lang.ref.WeakReference;

public class SplashScreen {
private static Dialog mSplashDialog;
private static WeakReference<Activity> mActivity;
private static WeakReference<ReactActivity> mActivity;

/**
* Show the splash screen.
*/
public static void show(final Activity activity) {
public static void show(final ReactActivity activity, final ReactInstanceManager instanceManager) {
if (activity == null) return;

// Store weak-reference to showing activity (in case we try to hide too early)
// NOTE: For instance in direct execution of index.android.js
mActivity = new WeakReference<Activity>(activity);
mActivity = new WeakReference<ReactActivity>(activity);

activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (!activity.isFinishing()) {
mSplashDialog = new Dialog(activity, R.style.RNSplashScreen_SplashTheme);
mSplashDialog.setCancelable(false);

if (!mSplashDialog.isShowing()) {
mSplashDialog.show();
}

// If given an instance manager; ensure that we transition to the stage-2
// splash screen
// TODO: If you think of a better way to do this; PR please
if (instanceManager != null) {
// A non-null context means that we are resuming from a
// background state and we will not get the context created event
ReactContext ctx = instanceManager.getCurrentReactContext();
if (ctx != null) {
activity.getWindow().getDecorView().setBackgroundColor(Color.WHITE);
} else {
// Else; wait until react is initialized before we release the native splash
instanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
@Override
public void onReactContextInitialized(ReactContext context) {
// Hide the native splash screen
activity.getWindow().getDecorView().setBackgroundColor(Color.WHITE);
}
});
}
}
}
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mehcode.reactnative.splashscreen;

import com.facebook.react.ReactActivity;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
Expand All @@ -19,7 +20,7 @@ public String getName() {
*/
@ReactMethod
public void show() {
SplashScreen.show(getCurrentActivity());
SplashScreen.show((ReactActivity)getCurrentActivity(), null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,9 @@ public class MainActivity extends ReactActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// Show the js-controlled splash screen
SplashScreen.show(this);

// After react is initialized; set our background color (override splash screen theme)
getReactNativeHost().getReactInstanceManager().addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
@Override
public void onReactContextInitialized(ReactContext context) {
// Hide the native splash screen
getWindow().getDecorView().setBackgroundColor(Color.WHITE);
}
});
// NOTE: It needs the react instance manager so it can listen to the react
// context creation event and hide the native splash
SplashScreen.show(this, getReactInstanceManager());

super.onCreate(savedInstanceState);
}
Expand Down
Binary file modified example/android/app/src/main/res/drawable/splash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.android.tools.build:gradle:2.2.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Jul 21 02:54:06 PDT 2016
#Fri Jan 20 16:10:09 PST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip

0 comments on commit f8bfc54

Please sign in to comment.