Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transform the context of fragment into a LifecycleOwner?

I have the following scenario. I have an activity which holds a fragment. In this fragment I'm displaying some records from a back-end database. I'm also using an adapter that looks like this:

public class MovieAdapter extends PagedListAdapter<Movie, MovieAdapter.MovieViewHolder> {
    private Context context;

    public MovieAdapter(Context context) {this.context = context;}

    @NonNull
    @Override
    public MovieViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //Create the view
    }

    @Override
    public void onBindViewHolder(@NonNull final MovieViewHolder holder, int position) {
        Movie movie = getItem(position);
        String title = movie.title;
        holder.titleTextView.setText(title);

        MovieRepository movieRepository = new MovieRepository(context);
        LiveData<Movie> liveData = movieRepository.retrieveFavoriteMovie(movie.id);
        liveData.observe(context, m -> { //Error
            if(m != null) {
                boolean favorite = m.favorite;
                if(favorite) {
                    //Do something
                } else {
                    //Do something else
                }
            }
        });
    }

    class MovieViewHolder extends RecyclerView.ViewHolder {
        ImageView favoriteImageView;
        TextView titleTextView;

        MovieViewHolder(View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.title_text_view);                favoriteImageView = itemView.findViewById(R.id.favorite_image_view);
        }
    }
}

In the onBindViewHolder I'm trying to check if a specific movie exist in Romm database but I get this error:

Wrong 1st argument type. Found: 'android.content.Context', required: 'android.arch.lifecycle.LifecycleOwner'

So how to transform the context of fragment into a LifecycleOwner so I can use it as in argument in my method?

like image 381
Joan P. Avatar asked Jan 31 '19 12:01

Joan P.


People also ask

How do you use LifecycleOwner?

Every Activity has a Lifecycle. This Activity will be a Lifecycle Owner(any activity or fragment can be called a lifecycle owner). When an activity is initialized LifecycleOwner is the one that will be constructed initially. Any class that implements the LifeCycleOwner interface indicates that it has Android LifeCycle.

How many types of LifecycleOwner available in fragment when and which LifecycleOwner should pass?

When using a ViewModel exposing LiveData s for a Fragment , there are currently two LifecycleOwners that can be used to oberve: the Fragment itself or. the Fragment 's property mViewLifecycleOwner .

What is LifecycleOwner?

ProcessLifecycleOwner. 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.

Is activity a lifecycle owner?

Android provides built-in components that are lifecycle owners. For activities, ComponentActivity , which is the base class for AppCompatActivity , is the one that implements LifecycleOwner . However, there are other classes that already implement this interface, too. For example, Fragment is a LifecycleOwner .


1 Answers

android.content.Context does not implement android.arch.lifecycle.LifecycleOwner.

You'd have to pass an instance of AppCompatActivity, which implements android.arch.lifecycle.LifecycleOwner (or any other class which does that).

or cast (AppCompatActivity) context, when context is an instanceof AppCompatActivity. To get the Activity from the Context in a reliable manner, check https://stackoverflow.com/a/46205896/2413303

like image 58
Martin Zeitler Avatar answered Oct 01 '22 22:10

Martin Zeitler