0

I am trying to implement LifecycleObserver on a custom RelativeLayout View. I am able to get the ON_RESUME event but for some reason the ON_PAUSE or ON_DESTROY event doesn't get triggered.

public class MyView extends RelativeLayout implements View.OnTouchListener, LifecycleObserver {

       public MyView (Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        setOnTouchListener(this); 
        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onViewPause()
    {
        Log.e("ON_PAUSE","ON_PAUSE");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onViewResume()
    {
        Log.e("ON_RESUME","ON_RESUME");
    }
}

What am I missing?

stavros.3p
  • 2,244
  • 6
  • 20
  • 37
  • You're using `ProcessLifecycleOwner` which is for interacting with the lifecycle of the Application process. I think you're intending to interact with the lifecycle of the Activity. – Sammy T Apr 08 '20 at 01:04
  • @SammyT What should I use instead of ProcessLifeCycleOwner? – stavros.3p Apr 08 '20 at 07:39
  • If you can pass an instance of `AppCompatActivity` into your view, you should be able to call `getLifecycle()` on it. – Sammy T Apr 09 '20 at 00:28
  • @SammyT Passing an instance is what I try to avoid – stavros.3p Apr 09 '20 at 00:49
  • Well, you can try passing in just the Lifecycle instead and continuing to add the observer from within the view. – Sammy T Apr 09 '20 at 01:07
  • I just need the onPause event so if I need to connect the activity or fragment I can do it easily using it's default methods onPause(). I am trying to avoid any additional code in Fragment or Activity. I used onDetachedFromWindow() inside the View instead, but sometimes it doesn't get called. – stavros.3p Apr 09 '20 at 01:21

0 Answers0