如何在 React Native 中显示用 java 创建的活动的模态

How to show a modal in React Native on an activity created in java

提问人:danielm2402 提问时间:7/19/2023 更新时间:7/19/2023 访问量:87

问:

我有一个应用程序,我在其中有一个模态。这个应用程序有一个我从 React Native 调用的原生应用程序。这有一些特别之处,因为它挂载了一个(android trust web activity)。问题是,当我打开模态时,模态没有显示(就好像它在 )。我怎样才能使模态显示在上方?React Native<App>fragmentfragmentTWATWATWA

我的模态App.js

import Modal from 'react-native-modal';

<Modal
            isVisible={open}
            onBackdropPress={() => {
              setOpen(false);
              setNotification({ title: '', body: '', topic: '', image: null });
            }}
          >
            <View style={{ backgroundColor: 'white', zIndex: 9999 }}>
              <Text style={{color:'#000'}}>{notification.title}</Text>
              <Text>{notification.body}</Text>
            </View>
          </Modal>

我的片段

public class MyFragment extends Fragment {
    private static final Uri LAUNCH_URI =
            Uri.parse("https://pwa.com");
    private final TrustedWebActivityIntentBuilder builder = new TrustedWebActivityIntentBuilder(
            LAUNCH_URI);
    private CustomTabsCallback mCustomTabsCallback = new QualityEnforcer();

    private List<TwaLauncher> launchers = new ArrayList<>();

    private final CustomTabsServiceConnection customTabsServiceConnection = new CustomTabsServiceConnection() {
        CustomTabsSession mSession;
        private final static int SESSION_ID = 45;  // An arbitrary constant.

        @Override
        public void onCustomTabsServiceConnected(ComponentName name,
                                                 CustomTabsClient client) {
            mSession = client.newSession(null, SESSION_ID);

            if (mSession == null) {
                Toast.makeText(getActivity(),
                        "Couldn't get session from provider.", Toast.LENGTH_LONG).show();
            }

            Intent intent = builder.build(mSession).getIntent();
            intent.putExtra(Intent.EXTRA_REFERRER,
                    Uri.parse("android-app://com.google.androidbrowserhelper?twa=true"));
            startActivity(intent);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mSession = null;
        }
    };

    private boolean serviceBound = false;

    public void launch() {
        TwaLauncher launcher = new TwaLauncher(getActivity());
        launcher.launch(LAUNCH_URI);
        launchers.add(launcher);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_blank, parent, false);

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // do any logic that should happen in an `onCreate` method, e.g:
        // customView.onCreate(savedInstanceState);
        launch();
    }

    @Override
    public void onPause() {
        super.onPause();
        // do any logic that should happen in an `onPause` method
        // e.g.: customView.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        // do any logic that should happen in an `onResume` method
        // e.g.: customView.onResume();
        launch();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        for (TwaLauncher launcher : launchers) {
            launcher.destroy();
        }
        // do any logic that should happen in an `onDestroy` method
        // e.g.: customView.onDestroy();
    }
}

调用片段的组件是这样的

import { MyViewManager } from './MyViewManager'


const createFragment = viewId =>
    UIManager.dispatchViewManagerCommand(
        viewId,
        // we are calling the 'create' command
        UIManager.MyViewManager.Commands.create.toString(),
        [viewId],
    );

const MyNativeActivityScreen = () => {

    const ref = useRef(null);

    useEffect(() => {
        const viewId = findNodeHandle(ref.current);
        createFragment(viewId);
    }, []);

    return (
        <MyViewManager
            style={{
                // converts dpi to px, provide desired height
                height: PixelRatio.getPixelSizeForLayoutSize(200),
                // converts dpi to px, provide desired width
                width: PixelRatio.getPixelSizeForLayoutSize(200),
            }}
            ref={ref}
        />
    );

};

export default MyNativeActivityScreen;
android react-native android-fragments trusted-web-activity

评论


答: 暂无答案