Nativescript: Android: programmatic app restart & killing worker

Created on 7 Feb 2017  Â·  11Comments  Â·  Source: NativeScript/NativeScript

So I've been fighting a long time with this one in various NS versions.
When my app starts, I start a worker which keeps periodically giving me data from a server.
When I make ONLY html changes somewhere in my app, the app restarts, yet the worker keeps running, which presents a problem - it is calling a function in one of my components successfully, but all that components variables exist no more, so I am getting an error (not important what error).
It is more of an annoyance, rather than something serious, because it only happens in development environment and I just have to restart the app either myself or cancel the run in cli and start it anew.

I wasn't really paying attention to this, until today, when somehow, a client on his phone got to the same error as I when I make this html change and CLI automatically restarts the app. In clients case, the release app just crashes of course.

Long story short - I just want to prevent this very rare occasion, where client would get in this error stage. Therefore, at that point, I would simply like to do a programmatic app restart.

I have been trying with this one:

var app = require("application");
...
var intent = app.android.context.getPackageManager().getLaunchIntentForPackage( app.android.context.getPackageName() );
intent.addFlags( android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP );
app.android.foregroundActivity.finish();
app.android.context.startActivity(intent);

.. but it doesn't truly restart the app, it keeps starting and quitting in a cycle after that, because my function that restarts the app keeps being called, because that old frikin worker is still running.

So the question is twopart:

  • what would be the correct way to restart an application programmatically in NS?

    • how can I make that worker stop working?

Most helpful comment

Thank youuuu! Your code worked like a charm! It was weird, that it kept throwing me error about parseInt, it didn't like the fact that pid is a number, so I just removed parseInt and it works:

var mStartActivity = new android.content.Intent(application.android.context, application.android.startActivity.getClass());
var mPendingIntentId = Math.random() * 100000;
var mPendingIntent = android.app.PendingIntent.getActivity(application.android.context, mPendingIntentId, mStartActivity, android.app.PendingIntent.FLAG_CANCEL_CURRENT);
var mgr = application.android.context.getSystemService(android.content.Context.ALARM_SERVICE);
mgr.set(android.app.AlarmManager.RTC, java.lang.System.currentTimeMillis() + 100, mPendingIntent);
android.os.Process.killProcess(android.os.Process.myPid());

All 11 comments

@dxshindeo - My plugin; Nativescript-master-technology has had the ability to restart (or quit) an application for a while. ;-) You can't currently restart an iOS app (apple doesn't let you), so it will quit it. But on Android it will restart the app.

Thank youuuu! Your code worked like a charm! It was weird, that it kept throwing me error about parseInt, it didn't like the fact that pid is a number, so I just removed parseInt and it works:

var mStartActivity = new android.content.Intent(application.android.context, application.android.startActivity.getClass());
var mPendingIntentId = Math.random() * 100000;
var mPendingIntent = android.app.PendingIntent.getActivity(application.android.context, mPendingIntentId, mStartActivity, android.app.PendingIntent.FLAG_CANCEL_CURRENT);
var mgr = application.android.context.getSystemService(android.content.Context.ALARM_SERVICE);
mgr.set(android.app.AlarmManager.RTC, java.lang.System.currentTimeMillis() + 100, mPendingIntent);
android.os.Process.killProcess(android.os.Process.myPid());

how to force stop android application not only my application,
any idea

@dxshindeo Does killProcess clean up Application context as well? Or there can be memory leaks?

@IgorGanapolsky - My understanding of killProcess is that the app is gone, including any contexts. I have not seen any issues with my code when restarting an app with the old one hanging around.

@NathanaelA - How do you go about using the "nativescript-master-technology" plugin within an NS Angular app. I've used required("plugin-name") within other parts of my application, but it seems like I cannot access your functions?

`
var masterTechnology = require("nativescript-master-technology");
export class SystemService {

public restartApp() {
masterTechnology .process.restart();
}
}
`

This returns an error of masterTechnology not defined.

@RichieRich69 use global.process.restart();
and no need to include the library like this:
var masterTechnology = require("nativescript-master-technology");

just add it like this:
require("nativescript-master-technology");

Perfect thank you!

On Mon, Feb 5, 2018 at 3:53 PM, Mohammad Opada Al Bosh <
[email protected]> wrote:

@RichieRich69 https://github.com/richierich69 use
global.process.restart();
and no need to include the library like this:
var masterTechnology = require("nativescript-master-technology");

just add it like this:
require("nativescript-master-technology");

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/NativeScript/NativeScript/issues/3602#issuecomment-363090633,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ALWT993SkhpNuBLa_27_lEMxo_xCLDRhks5tRwfigaJpZM4L5uEP
.

Can anyone know this issue.

Reference file is not getting created here "./node_modules/nativescript-master-technology/references.d.ts" after installing the plugin.

And, to test the plugin, I cloned this project in mine and testing it in android. But process.restart() says Error: getClass is not defined.

Any solution for this ?

@s-doddapaneni -
I would recommend you drop a issue into my repo w/ a test case and then when I have a chance I'll look into it. To my knowledge this code still works fine. But a test case helps me to narrow down what case it might be failing on...

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rogangriffin picture rogangriffin  Â·  3Comments

pocesar picture pocesar  Â·  3Comments

yclau picture yclau  Â·  3Comments

OscarLopezArnaiz picture OscarLopezArnaiz  Â·  3Comments

kn9ts picture kn9ts  Â·  3Comments