Open ghost opened 6 years ago
I am experiencing a similar issue on Android. On my side it's reproducible when I tap the icon to bring the running app back to foreground - bringing it back from the task manager opens the app in the state that I backgrounded it in, without showing the splash screen (so working as expected).
When I tap the icon, the functionality looks like it is reloading the app, as it it were not backgrounded. In my implementation, the splash screen successfully hides in this scenario, but as mentioned above, the functionality appears different than when I bring it back to foreground using the task manager. So, I'd expect given my experience that your "being stuck" at the splash screen might be implementation related.
If anybody has thoughts on this it would be greatly appreciated. Next step for me is to load a sample project with the splash a screen to ensure it is not implementation related on my side. It anybody has any input prior to that, it would be greatly appreciated.
I'm running: react-native 0.55.3 react-native-splash-screen 3.0.6
Thanks!
@syntaxerror1002 @andrewhamill
I also just ran into this issue as well. Did some Googling and found that you can address this by setting the main activity launchMode
as singleTop
in AndroidManifest.xml
.
Open path/to/project/android/app/src/main/AndroidManifest.xml
and add the following to the <activity>
.
<manifest ...
<application
...
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop" <-- Add this line
...
Make sure your Android device is connected and reinstall.
react-native run-android
Open the application, press Home button, navigate to the app icon and reopen the app by pressing the icon. You will not see the splash twice.
Here is the Android documentation that discusses this. https://developer.android.com/guide/topics/manifest/activity-element
The "standard" and "singleTop" modes differ from each other in just one respect: Every time there's a new intent for a "standard" activity, a new instance of the class is created to respond to that intent. Each instance handles a single intent. Similarly, a new instance of a "singleTop" activity may also be created to handle a new intent. However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created.
I think we can close this since it's Android by design, but it should be noted in the README.
With launchMode
set to singleInstance
, when the app is hidden pushing back button and restored with the launcher icon, the splash screen is redisplayed and must be hidden again. One can use:
AppState.addEventListener('change', () => { SplashScreen.hide(); });
With
launchMode
set tosingleInstance
, when the app is hidden pushing back button and restored with the launcher icon, the splash screen is redisplayed and must be hidden again. One can use:AppState.addEventListener('change', () => { SplashScreen.hide(); });
this works, thanks
@jay1337 I thought and did the same solution as you propose but it didn't work for me. Maybe because I'm using react-native-navigation (v2).
EDIT: I mean AppState.addEventListener('change', () => { SplashScreen.hide(); });
didn't work for me.
@FullstackJack thanks for the suggestion bud. I tried it and it works. But it doesn't seem to cover 100% of the cases since the Android app might decide to create a new instance from time to time. And I did reproduced that case randomly. As the documentation says:
Similarly, a new instance of a "singleTop" activity may also be created to handle a new
intent. However, if the target task already has an existing instance of the activity at
the top of its stack, that instance will receive the new intent (in an onNewIntent() call);
new instance is not created.
So it might that singleTask
/ singleInstance
values are better for the purpose we are discussing Then again it's not suggested for most apps by documentation. What do you think about it? Thanks.
@marudy I'd probably agree, but I'll have to try it. I noticed that with singleTop, new instances are created for resize events on split screen mode which resets the react-navigation state. I'll try these modes, thanks for the update.
@FullstackJack sure no problem. I run into the same issue these days and your answer was helpful for me. Let me know if you do have an update on it please. Are you also using react-native-navigation
?
@marudy I'm also having issues with react-native-navigation v2: Even though singleTop is set correctly, the splash screens stays. My current workaround seems to be working quite fine so far:
constructor(props: Props) {
super(props);
Navigation.events().bindComponent(this); // auto-unsubscribes
}
componentDidAppear() {
if (Platform.OS === 'android') SplashScreen.hide();
}
Edit: Globally this works as well:
if (Platform.OS === 'android')
Navigation.events().registerComponentDidAppearListener(() => {
SplashScreen.hide();
});
Dupe of #181.
Repro steps: 1) Install react-native-splash-screen per directions 2) Start the app 3) Hit the hardware back button until the app goes to the background 4) Bringing the app to the foreground by tapping its icon on the home screen or by resuming it via the task manager
Result: The splash screen opens when the app returns to the foreground and never goes away
I put some breakpoints in Android Studio and it appears that MainActivity OnCreate is being called every time the app returns to the foreground which shows the splash screen. However, since the react side of the app is already running, nothing executes to hide the splash screen as it did back when the app first started.
I tried modifying launchMode to singleTask and singleInstance but nothing helped. It seems a new MainActivity is created every time the app resumes from the background. All state within the react app remains intact.
I'm not sure if this is an issue w/ react-native-splash-screen, react-native itself, or just my app's setup.
Has anyone else encountered this?