Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fragment providing separate lifecycle owners for each created view hierarchy.
package be.digitalia.archcomponentsfix.fragment;
import android.arch.lifecycle.Lifecycle.Event;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.LifecycleRegistry;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
/**
* Fragment providing separate lifecycle owners for each created view hierarchy.
* <p>
* This is one possible way to solve issue https://github.com/googlesamples/android-architecture-components/issues/47
*
* @author Christophe Beyls
*/
public class ViewLifecycleFragment extends Fragment {
static class ViewLifecycleOwner implements LifecycleOwner {
private final LifecycleRegistry lifecycleRegistry = new LifecycleRegistry(this);
@Override
public LifecycleRegistry getLifecycle() {
return lifecycleRegistry;
}
}
@Nullable
private ViewLifecycleOwner viewLifecycleOwner;
/**
* @return the Lifecycle owner of the current view hierarchy,
* or null if there is no current view hierarchy.
*/
@Nullable
public LifecycleOwner getViewLifecycleOwner() {
return viewLifecycleOwner;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewLifecycleOwner = new ViewLifecycleOwner();
viewLifecycleOwner.getLifecycle().handleLifecycleEvent(Event.ON_CREATE);
}
@Override
public void onStart() {
super.onStart();
if (viewLifecycleOwner != null) {
viewLifecycleOwner.getLifecycle().handleLifecycleEvent(Event.ON_START);
}
}
@Override
public void onResume() {
super.onResume();
if (viewLifecycleOwner != null) {
viewLifecycleOwner.getLifecycle().handleLifecycleEvent(Event.ON_RESUME);
}
}
@Override
public void onPause() {
if (viewLifecycleOwner != null) {
viewLifecycleOwner.getLifecycle().handleLifecycleEvent(Event.ON_PAUSE);
}
super.onPause();
}
@Override
public void onStop() {
if (viewLifecycleOwner != null) {
viewLifecycleOwner.getLifecycle().handleLifecycleEvent(Event.ON_STOP);
}
super.onStop();
}
@Override
public void onDestroyView() {
if (viewLifecycleOwner != null) {
viewLifecycleOwner.getLifecycle().handleLifecycleEvent(Event.ON_DESTROY);
viewLifecycleOwner = null;
}
super.onDestroyView();
}
}
@vincent-paing

This comment has been minimized.

Copy link

@vincent-paing vincent-paing commented May 14, 2018

I can't seem to find handleLifecycleEventfun inside lifecycle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.