activity和fragment都有了仰禽,dialog不加一個(gè)怎么行呢氮墨?
什么是 LifeCycleOwner 建議自己百度纺蛆,它可以用在livedata,AutoDispose 來實(shí)現(xiàn)自動(dòng)取消rxjava任務(wù)等等,用途多多规揪。
但是傳參數(shù)卻需要LifeCycleOwner ,dialog是沒有的桥氏,于是我徒手寫了一個(gè)。
用法
Dialog dialog=new Dialog(this);
DialogLifeCycleOwner dialogLifeCycle=new DialogLifeCycleOwner(dialog);
//由于接管了onDismiss,因此需要監(jiān)聽則需要通過此方法來實(shí)現(xiàn)監(jiān)聽猛铅。
dialogLifeCycle.setProxyOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
//dialog close
}
});
LifecycleOwner owner=dialogLifeCycle;
//AutoDispose的用法
observe.to(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(owner))).subscribe(observer);
源碼
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;
import java.lang.ref.SoftReference;
/**
* Author:Lozn
* Email:qssq521@gmail.com
* 2021/12/4
* 10:33
*/
public class DialogLifeCycleOwner implements LifecycleOwner {
private static class MyView extends View {
public void setLifecycleRegistrySoftReference(SoftReference<LifecycleRegistry> lifecycleRegistrySoftReference) {
this.lifecycleRegistrySoftReference = lifecycleRegistrySoftReference;
}
SoftReference<LifecycleRegistry> lifecycleRegistrySoftReference=null;
public MyView(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return false;
}
public MyView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void onVisibilityAggregated(boolean isVisible) {
super.onVisibilityAggregated(isVisible);
}
@Override
protected void onWindowVisibilityChanged(int visibility) {
super.onWindowVisibilityChanged(visibility);
if(lifecycleRegistrySoftReference.get()!=null){
lifecycleRegistrySoftReference.get().setCurrentState(visibility==VISIBLE?Lifecycle.State.STARTED:Lifecycle.State.INITIALIZED);
//沒有pause,那么可以移動(dòng)到oncreate,或者onPause之前
}
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
if(lifecycleRegistrySoftReference.get()!=null){
lifecycleRegistrySoftReference.get().setCurrentState(hasWindowFocus?Lifecycle.State.RESUMED:Lifecycle.State.INITIALIZED);
//沒有pause,那么可以移動(dòng)到oncreate,或者onPause之前
}
}
}
public DialogLifeCycleOwner(Dialog dialog) {
lifecycleRegistry = new LifecycleRegistry(this);
lifecycleRegistry.setCurrentState(Lifecycle.State.CREATED);
/* dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
lifecycleRegistry.setCurrentState(Lifecycle.State.STARTED);
if (proxyOnShowListener != null) {
proxyOnShowListener.onShow(dialog);
}
}
});*/
MyView view = new MyView(dialog.getContext());
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(1, 1);
params.gravity= Gravity.RIGHT|Gravity.CENTER;
dialog.addContentView(view, params);
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
lifecycleRegistry.setCurrentState(Lifecycle.State.DESTROYED);
if (proxyOnDismissListener != null) {
proxyOnDismissListener.onDismiss(dialog);
}
}
});
}
public void setProxyOnDismissListener(DialogInterface.OnDismissListener proxyOnDismissListener) {
this.proxyOnDismissListener = proxyOnDismissListener;
}
/* public void setProxyOnShowListener(DialogInterface.OnShowListener proxyOnShowListener) {
this.proxyOnShowListener = proxyOnShowListener;
}*/
DialogInterface.OnDismissListener proxyOnDismissListener;
// DialogInterface.OnShowListener proxyOnShowListener;
private LifecycleRegistry lifecycleRegistry;
@Override
public Lifecycle getLifecycle() {
return lifecycleRegistry;
}
}