1

I've a simple application running Architecture Components. Since the latest update of the library ("Beta2"), i've an issue with my observable not being triggered (in a Fragment, the same code works on an Activity)

Here is a sample which is currently not working.

class SampleFragment : Fragment() {

    private var isDataReady = MutableLiveData<Boolean>()

    private val registry = LifecycleRegistry(this)

    override fun getLifecycle(): LifecycleRegistry = registry

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_main2, container, false)
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        fab.setOnClickListener { _ ->
            isDataReady.postValue(true)
        }
        isDataReady.observe(this, Observer {
            Snackbar.make(fab, "Hello!", Snackbar.LENGTH_LONG).show()
        })
    }
}

Did i missed something ?

Thanks for your help.

colletjb
  • 279
  • 3
  • 16
  • See if this helps you: https://stackoverflow.com/questions/45889604/livedata-is-not-updating-its-value-after-first-call – joao86 Oct 09 '17 at 08:52
  • Thanks. It used to work as long as LifecycleFragment was used. But it's now deprecated and its logic has moved to Fragment (from Support Library v26). I still have the issue though :( – colletjb Oct 09 '17 at 09:11
  • I haven't migrated to beta2. I will try it and see if my still works – joao86 Oct 09 '17 at 09:22
  • I saw that the changes in beta2 are valid if you use JAVA8 in your project. Do you use it ? – joao86 Oct 09 '17 at 09:44
  • I don't know which version of Java I use (i use Android Studio 3.0 beta), but i see it works with Activities and not Fragment. – colletjb Oct 09 '17 at 09:50
  • see this https://developer.android.com/guide/platform/j8-jack.html – joao86 Oct 09 '17 at 09:54

1 Answers1

0

I've finally figured out a solution on the above issue.

As we are in a Fragment, i must use the Activity to observe. (It should be "this" but it's not working so far.

isDataReady.observe(activity, Observer {

I guess it's a temporary fix...

Thanks for your help

colletjb
  • 279
  • 3
  • 16