1

I'm making an Android app, and I need to completely restart the app in a moment. So, to do that, the only solution that has worked (and that I can remember) is to use System.exit(0). I have no idea why I have this code there (I can't remember why I tried it or where I saw it or if it was just an accident), but I know it's working, and I now that I was reviewing the code to apply this on a service, it didn't start the activity I wanted (an activity that would completely stop the app and restart the service from zero - or a service that would do the same thing, but the service isn't called, nor the activity, so I don't know if it works by calling a service or not - an activity i know it works, at least from another activity):

  • MainActivity.java (in the point I want to restart it)
Intent MainRestarter= new Intent();
MainRestarter.setClass(MainActivity.this, MainRestarter.class);
MainRestarter.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
MainRestarter.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(MainRestarter);
System.exit(0);
  • MainRestarter.java
Intent MainActivity = new Intent();
MainActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
MainActivity.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
MainActivity.setClass(MainRestarter.this, MainActivity.class);
startActivity(MainActivity);
finish();

But I tested and it works too with:

  • MainActivity.java (in the point I want to restart it)
Intent MainActivity = new Intent();
MainActivity.setClass(MainActivity.this, MainActivity.class);
MainActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
MainActivity.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(MainActivity);
System.exit(0);

From an answer on another thread (Difference between finish() and System.exit(0)):

"System.exit(0) - restarts the app with one fewer activity on the stack. So, if you called ActivityB from ActivityA, and System.exit(0) is called in ActivityB, then the application will be killed and started immediately with only one activity ActivityA" - the difference here is that (using the names there) I call the ActivityB from ActivityA, and then I call System.exit(0) on ActivityA, and it restarts with ActivityB. But this doesn't work with a Service no idea why...

I thought this should just stop the entire app, but after reading that answer, it got me confused. Btw, it only works if I the startActivity() is called, or the app will close completely. Can anyone explain me why this code works, and then explain why it doesn't work if I try the exact same code in a Service?

Thanks in advance!

EDIT: I know I can do this calling AlarmManager before System.exit(0), but I'd just like to know why that works, and if I can keep using it instead of AlarmManager (on the MainActivity, since on the service, for some reason the above doesn't work and I'd like also to know why if anyone knows), because I read it consumes more battery.

Edw590
  • 447
  • 1
  • 6
  • 23
  • What is the purpose of needing to restart the app on the fly like that? – Chris K Apr 26 '19 at 19:05
  • I'm making a proxy server with a Go program incorporated in the app. And if I just stop the service it's running on, it doesn't stop. I've tried adding many things to the service's onDestroy() method, but the server doesn't stop. It's running on a Thread. I've tried interrupting it and equalling it to null, but it doesn't work either. And I've tried mixing stop the server with putting it to null and it didn't work. Only System.exit() works, so far. As I'm a begginer, I don't really know how to recreate the server in Java, so I'm trying with the Go program. And it looks it's the only option... – Edw590 Apr 26 '19 at 19:58

0 Answers0