Skip to content

Commit

Permalink
Merge pull request #3 from sospartan/android-bugfix-embedondisappear
Browse files Browse the repository at this point in the history
* [android] fix embed page appear event
  • Loading branch information
sospartan authored Jul 12, 2016
2 parents 03213f6 + b0a67e8 commit a84f07c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
22 changes: 22 additions & 0 deletions android/sdk/src/main/java/com/taobao/weex/WXSDKInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ public class WXSDKInstance implements IWXActivityStateListener {

private ViewGroup rootView;

public interface OnInstanceVisibleListener{
void onAppear();
void onDisappear();
}
private List<OnInstanceVisibleListener> mVisibleListeners = new ArrayList<>();

public WXSDKInstance(Context context) {
init(context);
}
Expand All @@ -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;

Expand Down Expand Up @@ -632,13 +646,20 @@ 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();
}
}
}

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();
}
}
}

Expand All @@ -647,6 +668,7 @@ public void onActivityResume() {
for (IWXActivityStateListener listener : mActivityStateListeners) {
listener.onActivityResume();
}
onViewAppear();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,23 @@
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;
import com.taobao.weex.dom.WXDomObject;
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);
}
Expand All @@ -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) {
Expand Down Expand Up @@ -312,6 +316,7 @@ public void setVisibility(String visibility) {
instance.onViewDisappear();
}
}
mIsVisible = visible;
}

@Override
Expand All @@ -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);
}
}
}

0 comments on commit a84f07c

Please sign in to comment.