-
Notifications
You must be signed in to change notification settings - Fork 24.9k

Description
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 ?