Avoid the lag caused while changing Fragment / Activity onNavigationItemSelected- Android


I have seen in many applications that when we change the option from the navigation menu, we observe that it lags, some people on StackOverflow recommended that use Handler like the below code:

But in the above code, it’s still not smooth & I thought why we add handler may be there is another solution after so much R&D, what I figure it out that we need to change the fragment/activity when the drawer is going to be close. let’s see with the implementation.

Now, what I did is, I created a variable with the name clickedNavItem with the dataType “int” and equals to the value of navigation menu when any item clicked.

After that in the onDrawerClose method, I checked the value clickedNavItem if it is equal to the selected menu id then open the activity or replace the fragment. That’s it, Implement this and see the difference.

If you want to learn more related to Android then check it out the below link:

Sharing Knowledge is Caring 😊

Thanks for reading this article. Be sure to clap to recommend this article if you found it helpful. It means a lot to me.

If you need any help then Join me on Twitter, Linkedin, Github, and Subscribe to my Youtube Channel.

Senior Software Engineer(Android). Open Source Contributor, Technical Writer, Email: Dani.amjad12@gmail.com check my website at https://danishamjad.com/

The (retired) Pub(lication) for Android & Tech, focused on Development