diff --git a/android/sdk/src/main/java/com/taobao/weex/WXSDKInstance.java b/android/sdk/src/main/java/com/taobao/weex/WXSDKInstance.java index 8417485870..10d3cdd145 100755 --- a/android/sdk/src/main/java/com/taobao/weex/WXSDKInstance.java +++ b/android/sdk/src/main/java/com/taobao/weex/WXSDKInstance.java @@ -290,6 +290,12 @@ public class WXSDKInstance implements IWXActivityStateListener { private ViewGroup rootView; + public interface OnInstanceVisibleListener{ + void onAppear(); + void onDisappear(); + } + private List mVisibleListeners = new ArrayList<>(); + public WXSDKInstance(Context context) { init(context); } @@ -305,6 +311,14 @@ public WXComponent getRootCom() { return ((WXVContainer) (this.getGodCom())).getChild(0); } + public void addOnInstanceVisibleListener(OnInstanceVisibleListener l){ + mVisibleListeners.add(l); + } + + public void removeOnInstanceVisibleListener(OnInstanceVisibleListener l){ + mVisibleListeners.remove(l); + } + public void init(Context context) { mContext = context; @@ -632,6 +646,10 @@ public void onViewDisappear(){ WXComponent comp = getRootCom(); if(comp != null) { WXBridgeManager.getInstance().fireEvent(this.mInstanceId, comp.getRef(), WXEventType.VIEWDISAPPEAR, null, null); + //call disappear of nested instances + for(OnInstanceVisibleListener instance:mVisibleListeners){ + instance.onDisappear(); + } } } @@ -639,6 +657,9 @@ public void onViewAppear(){ WXComponent comp = getRootCom(); if(comp != null) { WXBridgeManager.getInstance().fireEvent(this.mInstanceId, comp.getRef(), WXEventType.VIEWAPPEAR,null, null); + for(OnInstanceVisibleListener instance:mVisibleListeners){ + instance.onAppear(); + } } } @@ -647,6 +668,7 @@ public void onActivityResume() { for (IWXActivityStateListener listener : mActivityStateListeners) { listener.onActivityResume(); } + onViewAppear(); } @Override diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/component/WXEmbed.java b/android/sdk/src/main/java/com/taobao/weex/ui/component/WXEmbed.java index 3fd6406e4e..679e14c499 100755 --- a/android/sdk/src/main/java/com/taobao/weex/ui/component/WXEmbed.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/component/WXEmbed.java @@ -214,6 +214,7 @@ import com.taobao.weappplus_sdk.R; import com.taobao.weex.IWXRenderListener; import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.bridge.WXBridgeManager; import com.taobao.weex.common.WXDomPropConstant; import com.taobao.weex.common.WXPerformance; import com.taobao.weex.common.WXRenderStrategy; @@ -221,13 +222,15 @@ import com.taobao.weex.utils.WXLogUtils; import com.taobao.weex.utils.WXViewUtils; -public class WXEmbed extends WXDiv { +public class WXEmbed extends WXDiv implements WXSDKInstance.OnInstanceVisibleListener { private String src; private WXSDKInstance instance; private final static int ERROR_IMG_WIDTH = (int) WXViewUtils.getRealPxByWidth(270); private final static int ERROR_IMG_HEIGHT = (int) WXViewUtils.getRealPxByWidth(260); + private boolean mIsVisible = true; + public WXEmbed(WXSDKInstance instance, WXDomObject node, WXVContainer parent, boolean lazy) { super(instance, node, parent, lazy); } @@ -246,6 +249,7 @@ public void setSrc(String src) { private WXSDKInstance createInstance() { WXSDKInstance sdkInstance = new WXSDKInstance(mContext); + mInstance.addOnInstanceVisibleListener(this); sdkInstance.registerRenderListener(new IWXRenderListener() { @Override public void onViewCreated(WXSDKInstance instance, View view) { @@ -312,6 +316,7 @@ public void setVisibility(String visibility) { instance.onViewDisappear(); } } + mIsVisible = visible; } @Override @@ -323,4 +328,24 @@ public void destroy() { } src = null; } + + @Override + public void onAppear() { + //appear event from root instance will not trigger visibility change + if(mIsVisible && instance != null){ + WXComponent comp = instance.getRootCom(); + if(comp != null) + WXBridgeManager.getInstance().fireEvent(instance.getInstanceId(), comp.getRef(), WXEventType.VIEWAPPEAR,null, null); + } + } + + @Override + public void onDisappear() { + //appear event from root instance will not trigger visibility change + if(mIsVisible && instance != null){ + WXComponent comp = instance.getRootCom(); + if(comp != null) + WXBridgeManager.getInstance().fireEvent(instance.getInstanceId(), comp.getRef(), WXEventType.VIEWDISAPPEAR,null, null); + } + } } \ No newline at end of file