https://kotlinlang.org logo
Title
d

Damien O'Reilly

03/30/2019, 7:28 PM
Hi, anyone have issues when using databinding, and viewmodel's with mutable live data objects and fragments? e.g. on device rotate, I get
onCreateView
called twice, and 2nd time, my mutable live data is blank/fresh
s

Simon Schubert

03/30/2019, 8:00 PM
onCreateView gets called twice? Where and how are you adding the fragment to the activity?
d

Damien O'Reilly

03/30/2019, 8:03 PM
Its added like:
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_three)
        val host = NavHostFragment.create(R.navigation.my_navigation)
        supportFragmentManager
                .beginTransaction()
                .replace(R.id.my_fragment_container, host)
                .setPrimaryNavigationFragment(host)
                .commit()
    }
s

Simon Schubert

03/30/2019, 8:12 PM
The fragment will stay attached to the activity after rotation. Check if the onCreate gets called the first time and only then add the fragment
l

louiscad

03/30/2019, 9:08 PM
Just make the fragment transaction only
if (savedInstanceState == null)
d

Damien O'Reilly

03/30/2019, 10:26 PM
in the activities
onCreate
?
s

Simon Schubert

03/31/2019, 6:33 AM
Yes