Skip to content

Instantly share code, notes, and snippets.

@cbeyls
Last active June 29, 2020 14:06
Show Gist options
  • Save cbeyls/9ed37e8009e918cfe6df34fd17753b3c to your computer and use it in GitHub Desktop.
Save cbeyls/9ed37e8009e918cfe6df34fd17753b3c to your computer and use it in GitHub Desktop.
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
Copy link

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