更新Android Fragment中呈现的React原生组件的应用属性

wz3gfoph  于 2022-12-30  发布在  React
关注(0)|答案(1)|浏览(164)

bounty将在18小时后过期。回答此问题可获得+100声望奖励。YPD希望引起更多人关注此问题。

我已经在一个现有的Java Android项目中集成了一个React Native组件,它遵循了官方的documentation。一切都运行良好,我还能够传递一些属性:

public class NativeFragment extends Fragment {

    public NativeFragment() {}

    public static NativeFragment newInstance(String param1, String param2) {
            NativeFragment fragment = new NativeFragment();
            return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle initialProperties = new Bundle();
        initialProperties.putString("key", "Text string");

        Fragment reactNativeFragment = new ReactFragment.Builder()
                .setComponentName("MyApp")
                .setLaunchOptions(initialProperties)
                .build();

        getParentFragmentManager().beginTransaction().add(R.id.reactNativeFragment, reactNativeFragment).commit();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_native, container, false);
    }
}

现在我想更新onCreate方法中设置的应用属性。文档中提到的是Activity而不是Fragment,因此我不知道如何继续。文档建议使用ReactRootView,它用于呈现Activity中的React Native组件。
为了完整起见,下面的MainActivity呈现了一个包含两个片段的底部导航栏:一个片段是本机的,另一个是NativeFragment

public class MainActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_native)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);
    }

    @Override
    public void invokeDefaultOnBackPressed() {
        super.onBackPressed();
    }
}

那么,我该如何更新NativeFragment的应用属性呢?谢谢!

ufj5ltwl

ufj5ltwl1#

  • 问题:如何更新NativeFragment的应用属性?*

像这样:

要更新片段中React Native组件的属性,可以使用setArguments方法。
它看起来是这样的:

Bundle updatedProperties = new Bundle();
updatedProperties.putString("key", "Updated text string");

NativeFragment fragment = new NativeFragment();
fragment.setArguments(updatedProperties);

getParentFragmentManager()
.beginTransaction()
.replace(R.id.reactNativeFragment, fragment)
.commit();
  • 或者,* 您还可以通过调用setArgument方法来更新NativeFragment示例,然后使用FragmentManagerdetachattach方法重新呈现片段。

代码演练:

Bundle updatedProperties = new Bundle();
updatedProperties.putString("key", "Updated text string");

NativeFragment fragment = (NativeFragment) getParentFragmentManager()
.findFragmentById(R.id.reactNativeFragment);

fragment.setArguments(updatedProperties);

getParentFragmentManager()
.beginTransaction()
.detach(fragment)
.attach(fragment)
.commit();

相关问题