React Native Android:MainActivity继承两个类的冲突解决求助
Hey there! I’ve dealt with this exact conflict before when combining these two libraries, so let me break down the easiest ways to fix it—no deep Java expertise required.
Option 1: Use react-native-navigation's Built-in Splash Screen (Recommended)
Since you’re already extending SplashActivity from react-native-navigation, it’s simpler to ditch react-native-splash-screen entirely and use RNN’s native splash screen support. This eliminates the dependency conflict and keeps your setup cleaner.
Here’s how to set it up:
- First, uninstall
react-native-splash-screenif you’ve already added it:npm uninstall react-native-splash-screen # Or yarn remove react-native-splash-screen - Remove any
react-native-splash-screenrelated config from your Android project (like theSplashScreenActivityentry inAndroidManifest.xmland any splash drawables you added for it). - Create your splash screen drawable (a
splash_bg.xmlor PNG file) inandroid/app/src/main/res/drawable. - Update your
AndroidManifest.xmlto use a custom theme for yourMainActivity:<activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/SplashTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> - Add the
SplashThemetoandroid/app/src/main/res/values/styles.xml:<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowBackground">@drawable/splash_bg</item> <!-- Optional: Set your status bar color --> <item name="android:statusBarColor">@color/your_preferred_color</item> </style>
That’s it! RNN will automatically show this splash screen until your React Native app is ready to render.
Option 2: Keep Using react-native-splash-screen With RNN
If you really need to stick with react-native-splash-screen, you can still make it work without multi-inheritance. RNN’s SplashActivity handles the React Native initialization under the hood, so you just need to call the splash screen method in its lifecycle.
Here’s the modified MainActivity.java:
import com.reactnativenavigation.controllers.SplashActivity; import org.devio.rn.splashscreen.SplashScreen; import android.os.Bundle; public class MainActivity extends SplashActivity { @Override protected void onCreate(Bundle savedInstanceState) { // Show the react-native-splash-screen before calling super.onCreate SplashScreen.show(this); super.onCreate(savedInstanceState); } }
Then, in your React Native JavaScript code, call SplashScreen.hide() once your app is fully loaded (just like you would normally with react-native-splash-screen).
Why This Works
RNN’s SplashActivity is already built to bootstrap your React Native app, so you don’t need to extend ReactActivity directly. By adding SplashScreen.show(this) in the onCreate method, you’re just injecting the splash screen logic into RNN’s existing initialization flow.
内容的提问来源于stack exchange,提问作者jpls93

