'Fragments show before Views finish rendering in Android

I have a problem in my Android app: lately, when I switch between fragments, their layout shows as XML before children views are shown correctly. For example, in a fragment's onCreateView, when I set a text in a TextView or change the visibility of a view, the fragment layout displays for a fraction of a second as defined in XML, before computing the changes I made programmatically.

I didn't change anything significant in the app, and I don't think this is related to the Android version, because I still have older versions of my app installed in emulators that work fine, and when I install the new version it doesn't work correctly. Maybe it's related to the Android studio version, because no matter how much I try to go back to the old versions, nothing seems to work.

What I want is to have my app as it was before: every fragment layout is displayed once the stuff changed programmatically in onCreateView has finished (and not before).

Has anyone else experienced this weird behavior?



Solution 1:[1]

I think your problem is that you are setting the text in the fragment's onCreateView. You should set it on your fragment's onViewCreated.

Like this:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        # initialize
        mText = view?.findViewById(R.id.[codeTextView])!!
        mText.text = "test"
    }

Hope it works!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ladytoky0