RESTART automatically a nativescript 8 angular app after close -exit or finish - that app to change the app language

I'm implementing the EddyVerbruggen/nativescript-localize plug-in in my nativescript 8 angular app and I'm wondering if there is a way to automatically restart the app after an exit in IOS or a finish in android used to change the app language.

To be more precise, in the official NS documentation (https://docs.nativescript.org/plugins/localize.html#how-to-change-the-language-dynamically-at-runtime ) I found this code (see below alert ...) I wish to complete ????? (see code below) with a method (if is possible) that will RESTART the app after close.

import { Application } from '@nativescript/core'
import { overrideLocale } from '@nativescript/localize'

alert({
  title: 'Switch Language',
  message: 'The application needs to be restarted to change language',
  okButtonText: 'Quit!'
}).then(() => {
  L.localize.overrideLocale(selectedLang)
  if (isAndroid) {
    ;(
      Application.android.foregroundActivity || Application.android.startActivity
    ).finish()
    
    // Here I need a method to RESTART the app automatically after finsih
    ?????
  } else {
    exit(0)
    // Here I need a method to RESTART the app automatically after exit
    ?????
  }
})

Solution 1:

To restart the app in nativescript android, use this:

if (isAndroid) {
    const Intent = android.content.Intent;
    type Intent = android.content.Intent;
    
    const IntentCompat = androidx.core.content.IntentCompat;
    type IntentCompat = androidx.core.content.IntentCompat;

    const ctx: android.content.Context = application.android.context;

    const mainIntent: Intent = IntentCompat.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_LAUNCHER);
    mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    ctx.getApplicationContext().startActivity(mainIntent);
    (
        Application.android.foregroundActivity ||
        Application.android.startActivity
    ).finish();
}

For iOS, it seems that you can't restart the app, and Apple's recommendation is even not to exit the app programmatically using exit() as you can read here:

Warning: Do not call the exit function. Applications calling exit will appear to the user to have crashed, rather than performing a graceful termination and animating back to the Home screen.

However, if you still want to call the exit function, there are workarounds to restart the app such as scheduling a local notification to be displayed after the application exits (as appears here). To do this using nativescript you can use NativeScript Local Notifications Plugin using the following code:

import {
    LocalNotifications,
    NotificationAction
} from 'nativescript-local-notifications';

// inside your class:
private scheduleNotification() {
    var scheduledTime = new Date();
    scheduledTime.setSeconds(scheduledTime.getSeconds() + 30);
    
    const actionStartSession: NotificationAction = {
        id: 'startSession',
        type: 'button',
        title: 'Restart App',
        launch: true,
        choices: ['Restart']
    };

    // Check if we have permission on iOS
    LocalNotifications.hasPermission().then(granted => {
        if (granted) {
            console.log('We have permission to schedule notifications.');
        } else {
            LocalNotifications.requestPermission().then(granted => {
                alert(`This app needs permission to schedule notifications. Please go to iOS settings > hmapp > Notifications, and allow notifications.`);
            });
        }
    });

    LocalNotifications.schedule([
        {
            id: new Date().getTime(),
            title: 'App restart required',
            body: 'Tap to reopen the application',
            at: scheduledTime,
            actions: [actionStartSession]
        }
    ]).then(
        () => {
            console.log('Notification scheduled');
        },
        error => {
            console.log('Scheduling error: ' + error);
        }
    );
}