AppCompatActivity not implementing LifecycleOwner
Asked Answered
F

3

19

I am using Android Support Library 26.1.0. These are the dependencies in the app module:

implementation "android.arch.lifecycle:runtime:1.0.0"
implementation "android.arch.lifecycle:extensions:1.0.0-beta1"
implementation "android.arch.persistence.room:rxjava2:1.0.0-beta1"
implementation "android.arch.lifecycle:common-java8:1.0.0-beta1"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0-beta1"

But when I am trying to use ViewModel like the below:

mUserViewModel.getUsers().observe(this,
            (Observer<Resource<List<UserView>>>) listResource -> {
                if(listResource != null){
                    this.handleDataState(listResource.status, listResource.data, listResource.message);
                }
            });

It is showing error at this and the error message is Wrong first argument type Found: packagename.BrowseActivity, required: android.arch.lifecycle.LifecycleOwner. (Although support library version is 26.1+ which already implements LifecycleOwner)

I have also tried to implement LifecycleRegistryOwner which I found in this sample and this is also not working. Kindly suggest a solution and let me know if I am doing anything wrong.

Edit 1 - This is an Activity class:

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;

import com.github.amitkma.boilerplate.app.R;
import com.github.amitkma.boilerplate.app.mapper.UserMapper;
import com.github.amitkma.boilerplate.app.model.UserModel;
import com.github.amitkma.boilerplate.app.ui.widget.EmptyView;
import com.github.amitkma.boilerplate.app.ui.widget.ErrorView;
import com.github.amitkma.boilerplate.presentation.data.Resource;
import com.github.amitkma.boilerplate.presentation.data.ResourceState;
import com.github.amitkma.boilerplate.presentation.factory.ViewModelFactory;
import com.github.amitkma.boilerplate.presentation.viewmodel.UserViewModel;
import com.github.amitkma.boilerplate.presentation.vo.UserView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import dagger.android.AndroidInjection;

public class BrowseActivity extends AppCompatActivity {

    @Inject
    UserAdapter mUserAdapter;
    @Inject
    UserMapper mUserMapper;
    @Inject
    ViewModelFactory mViewModelFactory;
    private UserViewModel mUserViewModel;

    private RecyclerView mRecyclerView;
    private ProgressBar mProgressBar;
    private ErrorView mErrorView;
    private EmptyView mEmptyView;


   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       AndroidInjection.inject(this);
       setContentView(R.layout.activity_browse);
       mUserViewModel = ViewModelProviders.of(this, mViewModelFactory)
            .get(UserViewModel.class);

       mRecyclerView = findViewById(R.id.recycler_browse);
       mProgressBar = findViewById(R.id.progress);
       mEmptyView = findViewById(R.id.view_empty);
       mErrorView = findViewById(R.id.view_error);

       setupBrowseRecycler();
       setupViewListeners();
   }

    @Override
    protected void onStart() {
        super.onStart();
        mUserViewModel.getUsers().observe(this,
            (Observer<Resource<List<UserView>>>) listResource -> {
                if(listResource != null){
                    this.handleDataState(listResource.status, listResource.data, listResource.message);
                }
            });
    }
    ....
}

This is ViewModel Class:

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import com.github.amitkma.boilerplate.domain.interactor.GetUserList;
import com.github.amitkma.boilerplate.domain.model.User;
import com.github.amitkma.boilerplate.presentation.data.Resource;
import com.github.amitkma.boilerplate.presentation.data.ResourceState;
import com.github.amitkma.boilerplate.presentation.mapper.UserMapper;
import com.github.amitkma.boilerplate.presentation.vo.UserView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.subscribers.DisposableSubscriber;

public class UserViewModel extends ViewModel {

    private final GetUserList mGetUserList;
    private final UserMapper mUserMapper;

    @Inject
    public UserViewModel(GetUserList getUserList,
            UserMapper userMapper) {
        mGetUserList = getUserList;
        mUserMapper = userMapper;
        fetchUsers();
    }

    public void fetchUsers(){
        mUserMutableLiveData.postValue(new Resource(ResourceState.LOADING, null, null));
        mGetUserList.execute(new UserSubscriber(), null);
    }

    public LiveData<Resource<List<UserView>>> getUsers(){
        return mUserMutableLiveData;
    }

    @Override
    protected void onCleared() {
        mGetUserList.dispose();
        super.onCleared();
    }

    private final MutableLiveData<Resource<List<UserView>>> mUserMutableLiveData =
        new MutableLiveData<>();
}
Favianus answered 28/9, 2017 at 8:58 Comment(7)
You need to post more code. Show the Activity's imports and class definition.Verso
I have added the code. Please have a look.Favianus
I dont see any problem except that you import DisposableSubscriber in your ViewModel which is not needed.Wiles
I have not pasted the inner Subscriber class just because it is not related to this issue anyhow.Favianus
Please try to set the arch version to 1.0.0-alpha9-1.Wiles
I have set the version to 1.0.0-alpha9-1 and it worked and then again I replaced it with 1.0.0-beta1 and it is still working. No idea why it didn't work initially.Favianus
It's the Cache of the Annotation processor... . :P If my answers helped, accept it to help others. Thanks.Wiles
X
21

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

implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
Xanthene answered 14/4, 2019 at 19:41 Comment(0)
W
11

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

Wiles answered 28/9, 2017 at 9:10 Comment(4)
I have added activity and viewmodel class code. Kindly look into that.Favianus
Updated. Make sure to override getLifecycle().Wiles
I have already tried that. LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this); The problem is same. It is saying that LifecycleRegistry(LifecycleOwner) can not be applied to packageName.BrowseActivityFavianus
Ive updated my code and rebuilt a very quick example and it was working. Please try to invalidate your cache, and double-check your gradle file(s)Wiles
K
7

use android jetpack, it'll solve your problem

implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
Kleptomania answered 16/7, 2019 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.