Activity cannot be converted to LifecycleOwner
Asked Answered
G

2

10

I would like to use Room with LiveData, and in other projects I already used it, but in this one, I can not get it to work. 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):

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleRegistry;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

private LifecycleRegistry mLifecycleRegistry;

public class actMain extends AppCompatActivity  {

 @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mLifecycleRegistry = new LifecycleRegistry(this);
    mLifecycleRegistry.markState(Lifecycle.State.CREATED);
}
  @Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
    //Firebase
    db = FirebaseFirestore.getInstance();

    mLifecycleRegistry.markState(Lifecycle.State.STARTED);

    alarmViewModel = ViewModelProviders.of(this).get(AlarmViewModel.class);

    alarmViewModel.getAlarmList().observe(actMain.class, new 
    Observer<List<Alarm>>() {
        @Override
        public void onChanged(@Nullable List<Alarm> alarms) {

        }
    });
}
@NonNull
@Override
public Lifecycle getLifecycle() {
    return mLifecycleRegistry;
}

Here is my gradle file:

implementation 'androidx.room:room-runtime:2.0.0-alpha1'
annotationProcessor 'androidx.room:room-compiler:2.0.0-alpha1'
implementation 'com.google.android.material:material:1.0.0-alpha3'
implementation  'androidx.lifecycle:lifecycle-viewmodel:2.0.0-alpha1'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-alpha1'

And here is my Dao:

@Dao
public interface AlarmDao {

    @Query("SELECT * FROM alarm")
    LiveData<List<Alarm>> getAllAlarm();

    @Insert
    void insert(Alarm... alarms);

    @Update
    void update(Alarm... alarms);

    @Delete
    void delete(Alarm... alarms);

}

I tried every suggestion here including mine, but I can not figure out what is the issue in this case.

Edit: Code added

Giacomo answered 21/6, 2018 at 20:0 Comment(4)
Did you extend AppCompatActivity?Squawk
Yes ofc, I forget to add that part to my code.Chip
Support library 27 or 28? Although it seems to be androidx so probably 28Squawk
Yes, that's why I'm totally lost, I'm looking into two projects one is working and the other one is not...Chip
D
9

You don't need to use

mLifecycleRegistry = new LifecycleRegistry(this);
mLifecycleRegistry.markState(Lifecycle.State.CREATED);

Since, new AppcompatActivity is already lifecyclerOwner.

You also observe class object, which is incorrect. actMain.class is a class object. You should have:

alarmViewModel.getAlarmList().observe(this, new Observer<List<Alarm>>() {
     @Override
     public void onChanged(@Nullable List<Alarm> alarms) {}
});
Deflation answered 22/6, 2018 at 11:6 Comment(2)
I'm feeling very stupid know... but anyway thank you very much for your help! :)Chip
You've saved me! Thank you!Turnspit
E
0

Upgrade to latest version. Below is reference from my project AppCompatActivity now inherits from LifecycleOwner and you can straightaway implement the functionality you needed.

implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.1'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'org.jetbrains.anko:anko:0.10.8'
implementation 'com.github.debop:koda-time:1.2.1'


implementation 'androidx.annotation:annotation:1.0.2'
implementation "androidx.legacy:legacy-support-core-utils:1.0.0"


// Room components
implementation "androidx.room:room-runtime:2.0.0"
annotationProcessor "androidx.room:room-compiler:2.0.0"

// Lifecycle components
implementation "androidx.lifecycle:lifecycle-runtime:2.0.0"
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.0.0"

// Coroutines
implementation  "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1"
implementation  "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1"

Check for example here https://codelabs.developers.google.com/codelabs/android-room-with-a-view-kotlin/#13

Also you can refer the Basic example at https://github.com/googlesamples/android-architecture-components/tree/master/BasicSample

Equation answered 9/3, 2019 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.