Android: programmatic app restart & killing worker
See original GitHub issueSo 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?
Issue Analytics
- State:
- Created 7 years ago
- Comments:11 (3 by maintainers)
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:
@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.