Learning Android Development

7 Common Mistakes Easily Made with Android Fragment

You can eliminate these fragment issues with code review

Photo by krakenimages on Unsplash

A solid understanding of how Fragment works is essential when one working with Android Development. However, Fragment is still a complicated subject, and one can commonly miss out on something.

Mistake makes when working in Fragment sometimes are hard to debug, as it is not always replicable due to its complicated lifecycle event.

However, some of those issues could be easily prevented during coding review. Below are 7 of them

1. Create a new Fragment without checking savedStateInstance

In the Activity (or Fragment), if we have a Fragment as a view by default, then we can create it during onCreate as below.

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportFragmentManager.beginTransaction()
.replace(R.id.container, NewFragment())
.commit()
}

Why this is not good

--

--