zoontek / react-native-bootsplash

🚀 Show a splash screen during app startup. Hide it when you are ready.
MIT License
3.75k stars 259 forks source link

getApplicationContext().getPackageName() can return an inappropriate string #8

Closed MoOx closed 4 years ago

MoOx commented 5 years ago

💻 My environment

🕵️‍♂️ Reproducing the issue

If your code isn't located at the place expected by this assumption https://github.com/zoontek/react-native-bootsplash/blob/60f7e216d626264e72f5efdad97672162324de7f/android/src/main/java/com/zoontek/rnbootsplash/RNBootSplashActivity.java#L15-L16 (getApplicationContext().getPackageName())

(My case: white labeled app with dynamic application id that's doesn't match the classes path)

🤞Solution

Your solution after a private discussion:

https://developer.android.com/guide/topics/manifest/meta-data-element

    <activity
      android:name="com.zoontek.rnbootsplash.RNBootSplashActivity"
      android:theme="@style/BootTheme"> <!-- apply the theme you created at step 3. -->
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>

<meta-data android:name=“started” android:value=“com.my.ns” />
    </activity>

Workaround

Use your own class for the activity. Eg for me with android-pre x

package com.my.ns;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

public class RNBootSplashActivityPackageNameWorkaround extends AppCompatActivity {

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
      startActivity(new Intent(this, MainActivity.class));
      finish();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
      <activity
        android:name=".RNBootSplashActivityPackageNameWorkaround"
        android:theme="@style/SplashTheme"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
MoOx commented 5 years ago

Also if this fail, maybe do an explicit crash instead of just logging the stack trace? (Loggin is nice, but being stuck on the splash screen is probably more frustrating)

zoontek commented 5 years ago

@MoOx Totally agree that it should crash if the Activity is not found.

I will ship that feature and bump the package to v1.0.0 as it seems stable enough for everyone.

smakosh commented 5 years ago

Switching to another library as this doesn't work on Android nor on iOS

MoOx commented 5 years ago

It works like a charm. You probably didn't set this up correctly. If you share your setup we might be able to help you.

smakosh commented 5 years ago

I followed everything mentioned on the docs, iOS build succeeds but I'm still seeing the RN splash screen (I'm using react-navigation) As for Android, the build fails, getting this same issue, I tried the workaround mentioned above but the issue still exists

MoOx commented 5 years ago

If you share more details we won't be able to help.

For iOS

Can you show the snippet where [RNBootSplash show:@"LaunchScreen" inView:rootView]; is placed?

For Android

Please share the complete error

gorhom commented 5 years ago

hi @MoOx , i have created a pr to fix this issue but with different approach , hope it helps, https://github.com/zoontek/react-native-bootsplash/pull/16

MoOx commented 5 years ago

Not sure that your fix will help with my edge case but it looks good anyway!

zoontek commented 4 years ago

@MoOx The new version (3.0.0) no longer require a SplashScreenActivity.java! Please give it a try: https://github.com/zoontek/react-native-bootsplash/tree/3.0.0 🙂