使用 Android JNI 和片段的最佳实践是什么?

What is the best practice for using Android JNI and fragments?

使用 JNI 调用使用片段的应用程序的最佳实践是什么?

例如,我想使用主细节流模板(在此页面上向下滚动 https://developer.android.com/tools/projects/templates.html)来创建一个很好的项目列表,这些项目由来自 C (JNI) 端的调用填充。然而,这个模板使用了片段,这让我有点困惑,因为我对 Android 开发还比较陌生。

我应该在其中一个活动中创建一个 public 本机函数,还是应该在片段中?如果是 activity,我该如何在添加新项目时更新片段?

谢谢!

编辑:为清楚起见更改了问题标题。

Should I create a public native function in one of the activities, or should it be in the fragment?

将您的本机函数放在 activity 中。一般来说,Fragments 的生命周期比 Activity 短得多。此外,可以同时显示同一 Fragment 子类的多个实例。因此,通过在 activity 而不是片段中加载您的库,您总体上减少了系统的负载。

If it's the activity, how do I then make the fragment update whenever a new item gets added?

您可以使用 getSupportFragmentManager().getFragments(); 获得 List<Fragment>。从那里,您可以通过多种方式 select a fragment 并对其调用函数。