question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use transparent fragment by default

See original GitHub issue

On my react native App, I have a section that have look like this:

image

When you go to the next page, the blue image background remains fixed while the page go over.

We achieved this by doing something like that:

const ChapterWrapper = createStackNavigator(
  {
    "A": {
      screen: Question,
    },
    "B": {
      screen: Question,
    },
  },
  {
    cardStyle: {
      backgroundColor: 'transparent',
    },
    transitionConfig: () => ({
      // some custom animation, simplified because not impacting the issue
      containerStyle: {
        backgroundColor: 'transparent',
      },
    }),
  }
);

class QuestionsWrapper extends Component<
  IProps & IProfileCaptureContainerProps
> {
  public render() {
    return (
      <Fragment>
        <BackgroundContainer>
          <BackgroundImage chapter={this.getBackgroundChapterImage()} />
        </BackgroundContainer>
        <PageContainer>
          <ChapterWrapper navigation={this.props.navigation} />
        </PageContainer>
      </Fragment>
    );
  }
  private getBackgroundChapterImage = () => {
    const chapterLabel = this.props.navigation.getParam('chapterLabel');
  };
}

When installing react-native screen the Screen container have the default light grey android background that looks like this:

image

The explanation is that there is then this view hierarchie:

image

in this image, the background (in green) is between the first and the second view container (red). Since the background of them are not transparent (they have android default), the image is not shown. (my hypothesis, maybe I’m completely wrong).

I fixed this using patch package:

patch-package
--- a/node_modules/react-native-screens/android/src/main/java/com/swmansion/rnscreens/Screen.java
+++ b/node_modules/react-native-screens/android/src/main/java/com/swmansion/rnscreens/Screen.java
@@ -2,6 +2,7 @@ package com.swmansion.rnscreens;
 
 import android.annotation.SuppressLint;
 import android.content.Context;
+import android.graphics.Color;
 import android.os.Bundle;
 import android.support.annotation.Nullable;
 import android.support.v4.app.Fragment;
@@ -29,6 +30,7 @@ public class Screen extends ViewGroup {
     public View onCreateView(LayoutInflater inflater,
                              @Nullable ViewGroup container,
                              @Nullable Bundle savedInstanceState) {
+      mScreenView.setBackgroundColor(Color.TRANSPARENT);
       return mScreenView;
     }
   }

I’m happy to contribute to this repo but I will first discuss the solution

  • are transparent fragment hacky ?
  • should it be configurable somehow ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
kmagieracommented, Oct 18, 2018

I think you’ll have to do it here -> https://github.com/kmagiera/react-native-screens/blob/master/src/screens.native.js#L65

This could actually be the root cause, we seem to only pass styles to the wrapped Animated.View there and so the background isn’t set on the AnimatedNativeScreen component.

If you are on RN 0.57 you could try removing the whole wrapping bit and just do:

return <AnimatedNativeScreen {...this.props} />;

The whole wrapping code is workaround for one issue in react-native core that’s been fixed in 0.57 already with this change https://github.com/facebook/react-native/commit/75505993f0523ebf5f1ad68ee30e91e6879b28a3

1reaction
tychotacommented, Oct 17, 2018

(note, by default in iOS, it works, probably because the présentation mode is UIModalPresentationOverCurrentContext)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fragment has default white background on replace() instead ...
When I am replacing fragments inside my MainActivity container, some will keep the desired transparent background, and some will show a default- ...
Read more >
Using DialogFragment | CodePath Android Cliffnotes
DialogFragment is a specialized Fragment used when you want to display an overlay modal window within an activity that floats on top of...
Read more >
Android Fragment transparency problem - Android Forums
I find it funny in the 1st place that the Fragment backgrounds should come transparent by default ! I see a bunch of...
Read more >
Navigate between fragments using animations
The Fragment API provides two ways to use motion effects and transformations to visually connect fragments during navigation.
Read more >
Blending - LearnOpenGL
We want to discard the fragments that show the transparent parts of the ... This happens because OpenGL by default does not know...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found