Issue with using dialogs.confirm on first startup page of nativescript angular[Solved]


#1

There appears to be an issue when using dialogs on the first page of an app. I discovered this issue on my own app but I managed to reproduce it on a fresh app.

STR. Using TNS version 3.4.1

  1. create a fresh angular ie. tns create MyApp --ng
  2. On the start page (items.component.ts) in the ngOnInit() method add the following code.
ngOnInit(): void { this.items = this.itemService.getItems(); dialogs.confirm("Test Confirm").then(result => { console.log("Tapped Okay"); }) }
  1. run the app. ie. tns run android
  2. You get an exception in the terminal log. that ends with

ERROR Error: Uncaught (in promise): Error: java.lang.NullPointerException: Attempt to invoke virtual method ‘android.content.res.Resources$Theme android.content.Context.getTheme()’ on a null object reference.

  1. The confirm dialog doesn’t show up

On my own app I discovered that if you navigate to another page that uses a dialog in the OnInit after the first page is loaded it works fine and if you navigate back to the start page


#2

You have to wait for the activity to finish it’s initializations before it can show a dialog. OnInit doesn’t guarantee that page is loaded, you might want to wait till then.


#3

Which implementation should I use?

I’ve tried AfterViewInit, AfterContentInit and AfterViewChecked.

Whats interesting to me is that I’ve only just updated my nativescript-cli and tns modules recently. OnInit worked before 3.4


#4

If you want to show an alert as soon your application ready, you may try the page events or loaded event of action bar / any root view in that page. Remember, page events / loaded events will be triggered again when you navigate back from another page. If you don’t that behavior, don’t forget to remove the listeners.


#5

Still not 100% certain what you mean I’m still new to nativescript and haven’t done any direct development in java or objective c.

Are you talking about the native android loaded events or is there another event I can sign up to from the nativescript api? Also it looks like ios is also having issues loading the dialog as well but unfortunately there isn’t any errors :frowning:

I’m not sure which version it stopped working, I was previously on 3.1 and it was working fine but recently upgraded to 3.4 which broke it.


#6

@zanybanypany I never talked about listening to any native events. All events I talked about are inbox with NativeScript. Here is an example.

import { EventData } from "data/observable";
import * as dialogs from "ui/dialogs";
import { Page } from "ui/page";
....
....
    constructor(page: Page) {
        page.on(Page.loadedEvent, this.onPageLoaded, this);
    }

    onPageLoaded(event: EventData) {
        event.object.off(Page.loadedEvent, this.onPageLoaded, this);
        dialogs.confirm("Are you sure?")
            .then(() => {
                // Anything you might want to do
            });
    }

#7

Sorry for the late reply,

I’ve been super busy at work. I tried using the onPageLoaded event but I’m still getting the same issue. Are there any other alternatives to getting the confirm dialog on the first page?


#8

@zanybanypany Try navigatedTo event. It seems to be right option with latest version.


#9

Okay I’ve tried it and its working on the first page.

Thanks heaps for your time and patience manojdcoder

There seems to be another issue on the first navigation but I think its unrelated.