这是检测当前活动片段的好方法吗?

Is this a good way of detecting the current active fragment?

我目前正在尝试确定哪个片段是 onResume() 方法中的当前活动片段。

我目前正在根据目标 ID 进行检测。每个下载该应用程序的人的 ID 都是相同的还是随机生成的号码?

我如何获得 ID:

Navigation.findNavController(getView()).getCurrentDestination().getId()

我是如何使用它的:

 if (Navigation.findNavController(getView()).getCurrentDestination().getId() == 2131296382){
                Log.w("Navigation", "The current fragment is the chat fragment");
}

仅使用数字进行操作是非常糟糕的方法 - 在您的情况下为整数 ID,称为 'magic numbers'。

Would the ID be the same for everyone who downloads the app

所有设备上的资源 ID 都相同,这些设备具有相同版本的应用程序,不同版本可能具有不同的资源 ID,因为它们是在构建期间生成的代码。

如果您想使用资源 ID 进行操作,您需要通过引用从 R.java class 获取它们。 R.java class 中提供了所有资源 ID。在您的情况下 R.id. 来自您的 navigation.xml。有关资源 ID 的更多信息,请点击此处 https://developer.android.com/guide/topics/resources/providing-resources