Issue
I'm currently trying to determine which fragment is the current active one within the onResume() method.
I'm currently basing the detection off the destination ID. Would the ID be the same for everyone who downloads the app or is it a randomly generated number?
How I'm getting the ID:
Navigation.findNavController(getView()).getCurrentDestination().getId()
How I'm using it:
if (Navigation.findNavController(getView()).getCurrentDestination().getId() == 2131296382){
Log.w("Navigation", "The current fragment is the chat fragment");
}
Solution
This is very bad approach to operate just with numbers - integer ids in your case, called 'magic numbers'.
Would the ID be the same for everyone who downloads the app
Resource ids will be same on all devices, which have the same version of the app, different versions may have different resource ids, because they are code generated during the build.
If you want to operate with resources ids you need to get them from R.java class by reference. All resources ids are available in R.java class. In your case R.id.<your_fgagment_id> from your navigation.xml. More about resources ids here https://developer.android.com/guide/topics/resources/providing-resources
Answered By - Artem Chernousov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.