1
  • I have been doing InAppBrowser(Webview to load an url) in Nativescript. From nativescript, I want to use InAppbrowser native android webview code for some reason.
  • So I got one sample link to look out:

https://www.codeday.top/2017/10/23/52017.html

  • With the help of reference I tried to pass activity context from nativescript file to typescript.

Error : But I'm getting cannot convert object to context error in Command prompt.

  • Below I have posted the code what I have been tried so far:

app.component.ts:

var activity:any = android.app.Activity;

export class AppComponent {

    onTap(args: EventData) {     

        org.example.MyToast.showToast(activity);  --> Here I'm passing object.So it doesn't take activity context reference. That's why I'm getting error.
    }   

}                                                       

MyToast.java:

public class MyToast {

    public static void showToast(Context context) {  

        final Dialog openDialog = new Dialog(context);
   }
  }

I don't know how to pass activity context reference in nativescript to typescript file. Could anyone help me out.

3
  • instead of this var activity:any = android.app.Activity; use current class context var activity:any =this; Commented Jan 22, 2018 at 11:40
  • @HemantParmar I got that same issue. ERROR Error: Cannot convert object to Landroid/content/Context; at index 0 Commented Jan 22, 2018 at 11:46
  • ok try another this android.app.Activity.extend("your class with packge name") like for ex= "com.AppComponent". Commented Jan 22, 2018 at 11:52

1 Answer 1

2
+100

android.app.Activity is not an instance of the Activity, it's a class descriptor.

You can find information on accessing the current foreground activity at the official {N} docs: https://docs.nativescript.org/cookbook/application#tracking-the-current-activity

import * as app from "tns-core-modules/application";
const androidApp = app.android;

if (androidApp.foregroundActivity === androidApp.startActivity) {
    ////console.log("We are currently in the main (start) activity of the application");
}
Sign up to request clarification or add additional context in comments.

4 Comments

could you tell me what is the activity context instance here?
I just did - androidApp.foregroundActivity is the activity instance, activity extends context - meaning the activity instance IS also a context instance.
really very much thank you. I did exactly what you said .working fine
This answer as a godsend. Luckily the solution is this simple :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.