(Android) react-native-splash-screen practice - solve the problem of starting a white screen after react-native is packaged

1. Installation

npm i react-native-splash-screen --save or
yarn add react-native-splash-screen --save

2. Automatic configuration

react-native link react-native-splash-screen or rnpm link react-native-splash-screen

or 3. Manual configuration

3.1 android/settings.gradle

include ':react-native-splash-screen'   
project(':react-native-splash-screen').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-splash-screen/android')

3.2  android/app/build.gradle 

...
dependencies {
    ...
    compile project(':react-native-splash-screen')
}

3.3 MainApplication.java

// react-native-splash-screen >= 0.3.1 
import org.devio.rn.splashscreen.SplashScreenReactPackage;
// react-native-splash-screen < 0.3.1 
// import com.cboy.rn.splashscreen.SplashScreenReactPackage;
 
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 SplashScreenReactPackage() //here   ); } }; @Override public ReactNativeHost getReactNativeHost() { return mReactNativeHost; } }

3.4 MainActivity.java

import android.os.Bundle; // here 
import com.facebook.react.ReactActivity;
// react-native-splash-screen >= 0.3.1
import org.devio.rn.splashscreen.SplashScreen; // here
// react-native-splash-screen < 0.3.1
// import com.cboy.rn.splashscreen.SplashScreen; // here
 
public class MainActivity extends ReactActivity {
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        SplashScreen.show(this);  // here
        super.onCreate (savedInstanceState);
    }
    // ...other code
}

3.5 When app/src/main/res/layout下creatinglaunch_screen.xml 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/launch_screen">
</LinearLayout>

3.6 Add the launch_screen.png image under app/src/main/res/drawable/

3.7 Set transparent background android/app/src/main/res/values/styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <!--Set transparent background-->
        <item name="android:windowIsTranslucent">true</item>
    </style>
</resources>

3.8 react-native run-android fails prompt tool:replace allbackup

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="包名">

application node added
<application tools:replace="android:allowBackup" ...

3.9 Android flashback needs to add android/app/src/main/res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <color name="primary_dark">#660B0B0B</color> </resources>

https://github.com/crazycodeboy/react-native-splash-screen/issues/149

4. Usage

import SplashScreen from 'react-native-splash-screen'
 
export default class WelcomePage extends Component {
 
    componentDidMount() {
        // do stuff while splash screen is shown
        // After having done stuff (such as async tasks) hide the splash screen
 SplashScreen.hide();
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325297430&siteId=291194637