question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android: programmatic app restart & killing worker

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
dxshindeocommented, Feb 8, 2017

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());
2reactions
NathanaelAcommented, Oct 26, 2017

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to programmatically force a full app restart? e.g. kill, then ...
1) Force close/stop/kill your own app, from within your app. 2) Set a timer/intent that tells the OS to launch your app before...
Read more >
Interact programmatically with the Navigation component
The Navigation component provides ways to programmatically create and interact with certain navigation elements.
Read more >
Stopping and Restarting an Activity | Android Developers
When your activity receives a call to the onStop() method, it's no longer visible and should release almost all resources that aren't needed...
Read more >
Finding Resume and Restart Errors in Android Applications
Section 2 we present KR error examples, the Android restart ... ing apps of pause-and-resume or kill-and-restart operations. In Android, apps are structured ......
Read more >
Top 10 Most Common Mistakes That Android Developers Make
Killing one won't free the resources used by the others. This Android programming tutorial recommends the proper use of fragments to make your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found