Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get support fragment manager from ThemedReactContext #21356

Closed
ghost opened this issue Sep 27, 2018 · 7 comments
Closed

Cannot get support fragment manager from ThemedReactContext #21356

ghost opened this issue Sep 27, 2018 · 7 comments
Labels
Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@ghost
Copy link

ghost commented Sep 27, 2018

Environment

React Native Environment Info:

System:
OS: macOS High Sierra 10.13.4
CPU: x64 Intel(R) Core(TM) i5-5350U CPU @ 1.80GHz
Memory: 87.25 MB / 8.00 GB
Shell: 3.2.57 - /bin/bash

Binaries:
Node: 8.11.3 - /usr/local/bin/node
npm: 5.6.0 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman

SDKs:
iOS SDK:
Platforms: iOS 11.2, macOS 10.13, tvOS 11.2, watchOS 4.2

IDEs:
Android Studio: 3.1 AI-173.4907809
Xcode: 9.2/9C40b - /usr/bin/xcodebuild

npmPackages:
react: ^16.5.0 => 16.5.0
react-native: ^0.57.1 => 0.57.1

npmGlobalPackages:
create-react-native-app: 1.0.0
react-native-cli: 2.0.1
react-native-git-upgrade: 0.2.7

Description

I'm writing a custom native android library that exposes a support fragment for display. I created a simple view manager on the android side of react in which I return a placeholder layout (just a frame layout inside a parent linear layout to replace with the fragment layout later) in createViewInstance and later intend to add the library fragment inside receiveCommand on user action from the react side. However in order to add the fragment to the container I cannot get the support fragment manager from the ThemedReactContext obj that I capture in createViewInstance. Doing themedReactContextObj.getCurrentActivity() yields the deprecated android.app.FragmentManager. How do I get android.support.v4.app.FragmentManager instead in my view manager? Any help is appreciated.

Reproducible Demo

Here's the code I'm working with based off a few solutions on SO & blogs on getting native fragments to display. It works if I use a android.app.Fragment to display instead of android.support.v4.app.Fragment:

package com.awesome.awesomelib
        
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.awesome.awesomelib.AwesomeFragment;

import java.util.Map;

public class AwesomeViewManager extends SimpleViewManager<View> {

    public static final String REACT_CLASS = "RCTAwesomeView";
    public static final int REACT_COMMAND_CREATE = 1;

    private ThemedReactContext mContext = null;

    @Override
    public String getName() {
        return REACT_CLASS;
    }

    @Override
    protected View createViewInstance(ThemedReactContext reactContext) {

        mContext = reactContext;

        LinearLayout layout = (LinearLayout) LayoutInflater.from(reactContext).inflate(R.layout.ll_awesomeview_container, null);
        return layout;
    }

    @Nullable
    @Override
    public Map<String, Integer> getCommandsMap() {
        return MapBuilder.of("create", REACT_COMMAND_CREATE);
    }

    @Override
    public void receiveCommand(View view, int commandId, @Nullable ReadableArray args) {
        Log.d(REACT_CLASS, "receiveCommand: " + commandId);
        switch (commandId) {
            case REACT_COMMAND_CREATE:
                createFragment(view);
                break;
        }
    }

    private void createFragment(View view) {

        if(mContext != null) {

            // load the awesome lib fragment as container

            AwesomeFragment awFragment = new AwesomeFragment();
            //Here's where I'm stuck !!
            mContext.getCurrentActivity().getFragmentManager().beginTransaction().add(R.id.fl_awesomeview_container, awFragment).commit();
        }
    }
}

As getFragmentManager() is deprecated, how do I get the support fragment manager from the ThemedReactContext ?

@react-native-bot
Copy link
Collaborator

It looks like you are using an older version of React Native. Please update to the latest release, v0.57 and verify if the issue still exists.

The ":rewind:Old Version" label will be removed automatically once you edit your original post with the results of running react-native info on a project using the latest release.

@ghost
Copy link
Author

ghost commented Sep 27, 2018

Upgraded react-native but the issue still persists

@aMarCruz
Copy link

aMarCruz commented Oct 4, 2018

@akdsouza I have the same issue.
getFragmentManager is deprecated, but getSupportFragmentManager needs FragmentActivity, which (I think) is not used anymore by RN 0.57 ...what a mess!! and I do not understand why Google is forcing us to use android.support.v4.app.FragmentActivity

Have you found any workaround?

@ghost
Copy link
Author

ghost commented Oct 5, 2018

Hi @aMarCruz , I went with just changing the MainActivity of the react generated android project to extend the ReactFragmentActivity instead of the default ReactActivity. Note however that after initially upgrading to RN 0.57, I later downgraded back to RN 0.55.4. This was based on a discussion I had at the end of this PR=> #20602 which I believe is based on this PR=> #19950

So far I haven't seen any problems but I haven't tested extensively for side effects. It's worth a try.

@aMarCruz
Copy link

aMarCruz commented Oct 5, 2018

@akdsouza , I think for public plugins supporting both MainActivity and FragmentActivity is the only path. In my case (a dialog lib) it is not so hard, although it introduces some duplication of code as in the React Native implementation. Thanks.

@stale
Copy link

stale bot commented Jan 4, 2019

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as "For Discussion" or "Good first issue" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Jan 4, 2019
@stale
Copy link

stale bot commented Jan 20, 2019

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

@stale stale bot closed this as completed Jan 20, 2019
@facebook facebook locked as resolved and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

No branches or pull requests

2 participants