From 147ace91c1c8754fe750fe1c315cf247c14e792b Mon Sep 17 00:00:00 2001 From: sospartan Date: Tue, 12 Jul 2016 16:05:35 +0800 Subject: [PATCH 1/2] * [android] fix embed page appear event --- .../java/com/taobao/weex/WXSDKInstance.java | 22 ++++++++++++++++ .../com/taobao/weex/ui/component/WXEmbed.java | 25 ++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) 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..a98458d763 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,22 @@ 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(); + 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(); + WXBridgeManager.getInstance().fireEvent(instance.getInstanceId(), comp.getRef(), WXEventType.VIEWDISAPPEAR,null, null); + } + } } \ No newline at end of file From b0a67e885f3b3136b66077d1509d420d46e4b892 Mon Sep 17 00:00:00 2001 From: sospartan Date: Tue, 12 Jul 2016 16:45:21 +0800 Subject: [PATCH 2/2] * [android] fix NPE --- .../src/main/java/com/taobao/weex/ui/component/WXEmbed.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 a98458d763..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 @@ -334,7 +334,8 @@ public void onAppear() { //appear event from root instance will not trigger visibility change if(mIsVisible && instance != null){ WXComponent comp = instance.getRootCom(); - WXBridgeManager.getInstance().fireEvent(instance.getInstanceId(), comp.getRef(), WXEventType.VIEWAPPEAR,null, null); + if(comp != null) + WXBridgeManager.getInstance().fireEvent(instance.getInstanceId(), comp.getRef(), WXEventType.VIEWAPPEAR,null, null); } } @@ -343,7 +344,8 @@ public void onDisappear() { //appear event from root instance will not trigger visibility change if(mIsVisible && instance != null){ WXComponent comp = instance.getRootCom(); - WXBridgeManager.getInstance().fireEvent(instance.getInstanceId(), comp.getRef(), WXEventType.VIEWDISAPPEAR,null, null); + if(comp != null) + WXBridgeManager.getInstance().fireEvent(instance.getInstanceId(), comp.getRef(), WXEventType.VIEWDISAPPEAR,null, null); } } } \ No newline at end of file