Search code examples
androidnativescriptnativescript-angular

NativeScript navigate to another page from custom Android activity


I am developing an APP with wechat login feature. After getting approval from wechat it will call a custom activity of my NativeScript APP. I am getting response correctly but how can I move to another page instated of the home page after doing some verification. I am using NativeScript + Angular.

Sample code

getJSON("https://api.weixin.qq.com/sns/oauth2/access_token?appid=ID&secret=SECRET&code=" + res.code + "&grant_type=authorization_code").then((res) => {
    console.dir(res);
    // ===> here I want navigation
}, err => {
    console.dir(err);
})

I tried like this:

frame.topmost().navigate("src/app/login/login.component");

but getting error:

JS: Unhandled Promise rejection: Cannot set property '_moduleName' of undefined ; Zone: ; Task: Promise.then ; Value: TypeError: Cannot set property '_moduleName' of undefined TypeError: Cannot set property '_moduleName' of undefined

Please give me some suggestions. Thanks in advance :)


Solution

  • Fire an event from the Activity callback, something like

    import { android } from "tns-core-modules/application";
    
    ...
    
    public onResp(res: com.tencent.mm.opensdk.modelbase.BaseResp) {
        console.log("onResp");
        console.dir(res);
        androidApp.notify(<AndroidActivityEventData>{ eventName: 'wxapiresponse', object: android, activity: this });
    }
    

    In app component, listen to the event

    export class AppComponent implements OnInit {
        constructor(private ngZone: NgZone, private routerExtensions: RouterExtensions) {}
    
        ngOnInit() {
          application.android.on('wxapiresponse', this.wxApiResponse, this);
        }
    
        wxApiResponse() {
           // making sure the event callback runs inside Angular zone
           this.ngZone.run(() => {
              this.routerExtensions.navigate(...);
           });
        }
    }