Required lifecycle owner found Activity

lifecycleowner in viewmodel
android viewmodel lifecycle
android fragment get lifecycle state
android context lifecycle
livedata observe
lifecycleactivity android
viewmodel lifecycleobserver
android service lifecycleowner

I am using android library androidx.appcompat:appcompat:1.0.2. Working on a sample of codelabs work manager. I need to get live data from ViewModel and I am using this

    mViewModel!!.getOutputWorkInfo()?.observe(this, Observer<List<WorkInfo>> {
 })

but this variable shows error - Type mismatch. Required:Lifecycle Owner. Found:BlurActivity

I googled all says no need to extend lifecycle owner, by default appcompact activity implements lifecycle owner.

And I also worked this in another project, no issues found. I don't know why I am getting this error in this project.

`

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.RadioGroup

import com.bumptech.glide.Glide

import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.work.WorkInfo

class BlurActivity : AppCompatActivity() {

    private var mViewModel: BlurViewModel? = null
    private var mImageView: ImageView? = null
    private var mProgressBar: ProgressBar? = null
    private var mGoButton: Button? = null
    private var mOutputButton: Button? = null
    private var mCancelButton: Button? = null

    /**
     * Get the blur level from the radio button as an integer
     * @return Integer representing the amount of times to blur the image
     */
    private val blurLevel: Int
        get() {
            val radioGroup = findViewById<RadioGroup>(R.id.radio_blur_group)

            return when (radioGroup.checkedRadioButtonId) {
                R.id.radio_blur_lv_1 ->  1
                R.id.radio_blur_lv_2 ->  2
                R.id.radio_blur_lv_3 ->  3
                else -> 1
            }
        }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_blur)

        // Get the ViewModel
        mViewModel = ViewModelProviders.of(this).get(BlurViewModel::class.java)

        // Get all of the Views
        mImageView = findViewById(R.id.image_view)
        mProgressBar = findViewById(R.id.progress_bar)
        mGoButton = findViewById(R.id.go_button)
        mOutputButton = findViewById(R.id.see_file_button)
        mCancelButton = findViewById(R.id.cancel_button)

        // Image uri should be stored in the ViewModel; put it there then display
        val intent = intent
        val imageUriExtra = intent.getStringExtra(Constants.KEY_IMAGE_URI)
        mViewModel!!.setImageUri(imageUriExtra)
        if (mViewModel!!.imageUri != null) {
            Glide.with(this).load(mViewModel!!.imageUri).into(mImageView!!)
        }

        mViewModel!!.getOutputWorkInfo()?.observe(this, Observer<List<WorkInfo>> {
            // If there are no matching work info, do nothing
            if (it == null || it.isEmpty()) return@Observer

            // We only care about the first output status.
            // Every continuation has only one worker tagged TAG_OUTPUT
            val workInfo = it[0]

            val finished = workInfo.state.isFinished
            if (!finished) showWorkInProgress() else showWorkFinished()
        })


        // Setup blur image file button
        mGoButton!!.setOnClickListener { view -> mViewModel!!.applyBlur(blurLevel) }
    }

    /**
     * Shows and hides views for when the Activity is processing an image
     */
    private fun showWorkInProgress() {
        mProgressBar!!.visibility = View.VISIBLE
        mCancelButton!!.visibility = View.VISIBLE
        mGoButton!!.visibility = View.GONE
        mOutputButton!!.visibility = View.GONE
    }

    /**
     * Shows and hides views for when the Activity is done processing an image
     */
    private fun showWorkFinished() {
        mProgressBar!!.visibility = View.GONE
        mCancelButton!!.visibility = View.GONE
        mGoButton!!.visibility = View.VISIBLE
    }
}

`

Same problem here, so I had to update my androidx.appcompat dependency, like below:

implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'

no need to implement LifecycleOwner (as its implemented by default now {as mentioned by Darthcow})

Handling Lifecycles with Lifecycle-Aware Components, https://developer.android.com › reference › android › arch › lifecycle › Lif Required lifecycle owner found Activity. 114. January 25, 2019, at 12:50 PM. I am using android library androidx.appcompat:appcompat:1.0.2. Working on a sample of

After implementing LifeCycleOwner in Main Activity, error goes and work properly

Updated

Use latest androidx lib and u don't need to implement LifecycleOwner. Now it is implemented by default in ComponentActivity which AppcompatActivity implements

LifecycleOwner, These events can be used by custom components to handle lifecycle changes without implementing any code inside the Activity or the Fragment. For the sake of completeness, even after adding the dependencies and having properly migrated to androidx using the one-click refactoring, you would still get intellisense errors against your Kotlin code saying

This issue occurs when there is mismatch between appcompat and Lifecycle dependency.

Either use this set of dependencies:

implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version
implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'

Or:

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation "android.arch.lifecycle:extensions:$lifecycle_version"

Android Architecture Components: LifeCycle - Mina Samy, Events are dispatched with respect to the life cycle owner events such as span multiple lifecycle owner events, so for an activity it's considered in the that's necessary for the life cycle events that are dispatched before the And that's it for our simple example, full source code can be found on GitHub. It can't convert my activity into Lifecycle activity when I try to observe the livedata, however, I'm using the AppCompatActivity, and I even tried to Override the getLifecycle method (which worked for me in previous projects). I even tried with AndroidX but still the same issue :(Here my activity (Part of it):

Exploring the new Android Architecture Components library, Also, see Implementing LifecycleOwner in Custom Activities and The ViewModel class itself doesn't actually require too much input from our  Class that provides lifecycle for the whole application process. A class that has an Android lifecycle. These events can be used by custom components to handle lifecycle changes without implementing any code inside the Activity or the Fragment.

Incorporate Lifecycle-Aware Components, LiveData respects the complex lifecycles of your app components, including activities, fragments, services, or any LifecycleOwner defined in your app. LiveData  As a user navigates through, out of, and back to your app, the Activity instances in your app transition through different states in their lifecycle. The Activity class provides a number of callbacks that allow the activity to know that a state has changed: that the system is creating, stopping, or resuming an activity, or destroying the process in which the activity resides.

Make your Custom View Lifecycle-Aware: it's a piece of cake, Since every Activity/Fragment is now subclass for LifecycleOwner, you can get an instance of their Lifecycle with the getLifecycle() function and  The androidx.lifecycle package provides classes and interfaces that let you build lifecycle-aware components—which are components that can automatically adjust their behavior based on the current lifecycle state of an activity or fragment.

Comments
  • Where do you use this piece of code? this should be any LifeCycleOwner (i.e. Activity or Fragment)
  • activity, class BlurActivity : AppCompatActivity()
  • Try this code : mViewModel!!.getOutputWorkInfo()?.observe(this@BlurActivity, Observer<List<WorkInfo>> { })
  • same error getting
  • It's weird. In documentation androidx AppCompatActivity is not implementing LifeCycleOwner, but in my code (If I open its sources) it does.
  • please also mention that it's because it's already implemented by default now