AppCompatActivity not implementing LifecycleOwner

旧巷老猫 提交于 2019-11-29 13:13:42

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

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

You don't need to implement this anymore.

Just extend your activity by AppCompatActivity which extends LifecycleOwner.

This interface was deprecated in API level 1.0.0. Use android.support.v7.app.AppCompatActivity which extends LifecycleOwner, so there are no use cases for this class.

You forgot to override

private final LifecycleRegistry lifecycleRegistry = new LifecycleRegistry(this);

@Override
public LifecycleRegistry getLifecycle() {
    return lifecycleRegistry;
}

You need at least Support-Lib 26.1

Same for fragments. Just extend your Fragment by LifecycleFragment which extends android.support.v4.app.Fragment

Edit: Just rebuilt a quick sample project using Kotlin and it worked.

class MainActivity : AppCompatActivity(), HasSupportFragmentInjector, AnkoLogger {
    private val lifecycleRegistry by lazy { android.arch.lifecycle.LifecycleRegistry(this) }
    private val viewModel by lazy { ViewModelProviders.of(this, viewModelFactory).get(BridgesVm::class.java) }
    override fun getLifecycle() = lifecycleRegistry

    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel.data.observe(this, Observer<Bridge?> { info { "Received" } })
   }
}

class BridgesVm @Inject constructor() : ViewModel() { 
        val data: LiveData<Bridge> = MutableLiveData<Bridge>()
}

Edit:

There's a problem using arch version beta1. Consider using 1.0.0-alpha9-1 until this is fixed. In my case it wasnt working if i use beta1 but working with alpha 9-1

use android jetpack, it'll solve your problem

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!