Skip to content

NativeScript is working weird in 'cold start' stage | possibility of many bugs #8577

@plackowski

Description

@plackowski

Hi, I have a problem with my NativeScript app during the first start. I know there is a 'cold start' but activities are duplicated each time I click the home button on my device and then back to the app (I repeat, only after first app opening). This is a BIG bug (possibility of memory leak, duplicated requests, weird UI experiences because a user clicks on the back button and see the same pages).

As I know it can be old android bug. The first mentioned bug about this is from 2009, so I can't expect that google will fix it soon ;)

https://issuetracker.google.com/issues/64108432

https://discourse.nativescript.org/t/kill-app-when-pushed-to-background/3560

What I want to do is call the 'back button' when the app starts for the first time, because after this, the app is working properly. I found that I can use android.foregroundActivity.finish(), but when I try to do it, I get an error message that

TypeError: Cannot read property 'finish' of undefined

Tried with:

var app = require('application');
app.android.foregroundActivity.finish();

and

import { android } from 'tns-core-modules/application';
android.foregroundActivity.finish();

foregroundActvity is found, but not finish(). I also tried with android.os.Process.killProcess(android.os.Process.myPid()); but there is no property like 'android.os' :/

Any idea of how to use it properly? Or maybe there is another option to handle this problem? The whole main.tns.ts file below:

Any idea of how to use it properly? Or maybe there is another option to handle this problem? The whole main.tns.ts file below:

import { hasKey, setBoolean } from 'tns-core-modules/application-settings';
 import { platformNativeScriptDynamic } from 'nativescript-angular/platform';
 import { AppOptions } from 'nativescript-angular/platform-common';
 import {
   on as applicationOn,
   off as applicationOff,
   exitEvent,
   launchEvent,
   ApplicationEventData,
   android,
 } from 'tns-core-modules/application';
 import { enableProdMode } from '@angular/core';
 import { AppModule } from '~/app/app.module';
 
 const firebase = require('nativescript-plugin-firebase');
 
 let isAppInitialized = false;
 let options: AppOptions = {};
 
 function launchListener(args: ApplicationEventData) {
   if (args.android) {
     handleColdStart(args.android);
     tryToInitializeFirebase();
   }
 }
 
 function handleColdStart(context) {
   if (!hasKey('isFirstStart')) {
     setBoolean('isFirstStart', true);
     android.foregroundActivity.finish();
   }
 }
 
 function tryToInitializeFirebase() {
   if (!isAppInitialized) {
     setTimeout(() => {
       firebase
         .init({})
         .then((instance) => {
           console.log('firebase.init donessse');
         })
         .catch((err) => {
           console.log('Firebase init error: ' + err);
         });
     }, 1000);
     isAppInitialized = true;
   }
 }
 
 function exitListener(args: ApplicationEventData) {
   if (args.android) {
     applicationOff(launchEvent, launchListener);
   }
 }
 
 applicationOn(launchEvent, launchListener);
 applicationOn(exitEvent, exitListener);
 
 if (module['hot']) {
   options.hmrOptions = {
     moduleTypeFactory: () => AppModule,
     livesyncCallback: (platformReboot) => {
       setTimeout(platformReboot, 0);
     },
   };
 
   module['hot'].accept(['./app/app.module'], () => {
     global['hmrRefresh']();
   });
 }
 
 enableProdMode();
 platformNativeScriptDynamic().bootstrapModule(AppModule);

I created a project on a playground and the funny thing is that if you run this project via playground (online preview) it's working fine (I don't need to kill the process). But if you download that projects and then run this project with tns run android (with tns preview it's also working properly), you will see what I'm talking about:

https://play.nativescript.org/?template=play-ng&id=MxBIfX&v=2

I recorded video: https://www.youtube.com/embed/DJETWEd3EaY

I found https://www.nativescript.org/blog/nativescript-preview and "Since, the preview command skips the step of building a native app, anything that would usually require rebuilding the native app is where the limitations are." so maybe it's the reason why preview online (tns preview) works differently. Maybe there is possibility to change configuration somehow to fix it? Any idea?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions