LaraFlow
LaraFlow

Reputation: 392

Cannot access androidx.lifecycle.LifecycleOwner, what is the meaning of error in Android?

On which situation android-build/android-studio shows these errors?

Cannot access androidx.lifecycle.LifecycleOwner and
Cannot access androidx.lifecycle.ViewModelStoreOwner

What is the meaning of this errors? I found that adding androidx.lifecycle:lifecycle-viewmodel as dependency solves these errors, I want to know the meaning of the error in terms of Android Activity LifeCycle(Or is it related to lifecycle or not).

For example in this code call to `super' onSavedInstanceState shows this error

public class AppActivity extends FragmentActivity {

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);  // Cannot access androidx.lifecycle.LifecycleOwner
    }

}

When should I use Android Lifecycles,and when should I avoid using it?

Upvotes: 12

Views: 8491

Answers (4)

Aliakbar rockz
Aliakbar rockz

Reputation: 33

File > Invalidate Caches / Restart - Worked Actually.

Also I updated the gradle implementations with its latest version release.

Upvotes: 1

serhii
serhii

Reputation: 151

Update to the latest appcompat library version can help with this.

androidx.appcompat:appcompat:1.3.0-alpha01

Upvotes: 6

kakyo
kakyo

Reputation: 11640

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

Cannot access 'Cannot access androidx.lifecycle.LifecycleOwner' ...

, although the project builds fine.

Then you would have to

  1. Quit Android Studio
  2. Remove project/.idea folder
  3. Remove ~/.gradle folder
  4. Restart Android Studio

File > Invalidate Caches / Restart won't work.

Upvotes: 1

rham
rham

Reputation: 151

add this to your gradle dependencies

implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

Upvotes: 15

Related Questions