Fixed popup menu lifecycle

This commit is contained in:
M66B
2019-09-21 20:28:03 +02:00
parent e6d3999a4e
commit fade38f07d
9 changed files with 17 additions and 30 deletions

View File

@@ -30,24 +30,12 @@ import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;
public class PopupMenuLifecycle extends PopupMenu implements LifecycleObserver {
private LifecycleOwner owner;
public PopupMenuLifecycle(@NonNull Context context, LifecycleOwner owner, @NonNull View anchor) {
super(context, anchor);
this.owner = owner;
Log.i("Instantiate " + this);
anchor.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View v) {
// Do nothing
}
@Override
public void onViewDetachedFromWindow(View v) {
PopupMenuLifecycle.this.dismiss();
}
});
owner.getLifecycle().addObserver(this);
}
@Override
@@ -64,6 +52,5 @@ public class PopupMenuLifecycle extends PopupMenu implements LifecycleObserver {
public void onDestroy() {
Log.i("Destroy " + this);
this.dismiss();
owner = null;
}
}