Weex Android 返回键 页面传值 生命周期

问题

  1. 安卓返回键
  2. 页面之间传参数
  3. Vue页面生命周期

vue.js实现

<template>
//页面根节点
    <div @onAndroidBack="onAndroidBack" @ready="ready" @actived="actived" @deactived="deactived" class="container">
        
    </div>

</template>
<script>
methods: {
            //页面ready 页面传参
            ready(param){
                let p = JSON.stringify(e.param)
                console.log('pageA页面传参数=' + p);
            },
            //页面激活 param 页面传参
            actived(param){
                
            },
            //页面失活
            deactived(){
               
            },
            //
            onAndroidBack(){
                uNavigator.pop();
            },
            onClick(){
                uNavigator.popTo({index:-1,tagCode:"pageb",param:{test:'testB'}},() => {} );
            }
        },
        </script>

Android实现:

public void onBackPressed() {
        WXComponent comp = mInstance.getRootComponent();
        if (comp != null) {
            WXEvent events = comp.getDomObject().getEvents();
            boolean hasBack = events.contains(UConstants.Event.ONANDROIDBACK);
            if (hasBack) {
                WXBridgeManager.getInstance().fireEvent(mInstance.getInstanceId(), comp.getRef(), UConstants.Event.ONANDROIDBACK, null, null);
                return;
            }else {
                super.onBackPressed();
            }
        }
        super.onBackPressed();
    }
    
 @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        if (intent == null) {
            return;
        }
        if (intent.getStringExtra("params") != null) {
            String params = intent.getStringExtra("params");
            String backTag = intent.getStringExtra("backTag");
            if (!TextUtils.isEmpty(params)) {
                WXComponent comp = mInstance.getRootComponent();
                if (comp != null) {
                    WXEvent events = comp.getDomObject().getEvents();
                    boolean hasActive = events.contains(UConstants.Event.ACTIVED);
                    if (hasActive) {
                            Map<String, Object> data = new HashMap<>();
                            data.put("param",params);
                            data.put("tagCode", backTag);
                            WXBridgeManager.getInstance().fireEvent(mInstance.getInstanceId(), comp.getRef(), UConstants.Event.ACTIVED, data, null);
//                        }
                    }
                }
            }
        }
    }
@Override
    public void onViewCreated(WXSDKInstance wxsdkInstance, View view) {
        super.onViewCreated(wxsdkInstance, view);
        WXComponent comp = mInstance.getRootComponent();
        if (comp != null) {
            WXEvent events = comp.getDomObject().getEvents();
            boolean hasReady = events.contains(UConstants.Event.READY);
            if (hasReady) {
                Map<String, Object> data = new HashMap<>();
                data.put("param", wxInfo.getParam());
                WXBridgeManager.getInstance().fireEvent(mInstance.getInstanceId(), comp.getRef(), UConstants.Event.READY, data, null);
            }
        }
        if (!isHasNavBar) {
            setTranslateAnimation(getContainer());
        }
    }
 @Override
    public void onPause() {
        super.onPause();
        WXComponent comp = mInstance.getRootComponent();
        if (comp != null) {
            WXEvent events = comp.getDomObject().getEvents();
            boolean hasDeactived = events.contains(UConstants.Event.DEACTIVED);
            if (hasDeactived) {
                WXBridgeManager.getInstance().fireEvent(mInstance.getInstanceId(), comp.getRef(), UConstants.Event.DEACTIVED, null, null);
            }
        }
        if (mIsShakeDetectorStarted && mShakeDetector != null) {
            mShakeDetector.stop();
            mIsShakeDetectorStarted = false;
        }
    }
    @Override
    public void onResume() {
        super.onResume();
        if (!mIsShakeDetectorStarted && mShakeDetector != null) {
            mShakeDetector.start((SensorManager) getApplicationContext().getSystemService(Context.SENSOR_SERVICE));
            mIsShakeDetectorStarted = true;
        }
    }

开源:
https://github.com/weexext/we…

    原文作者:tracy
    原文地址: https://segmentfault.com/a/1190000010677572
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞