I have an app that include both a complex service and activity UI. The app runs on multiple devices which communicate with each other over WIFI via the service.
Since the app is a prototype/in-development, I want to add support for a "force restart" that will kill the app and re-launch it clean. There is a lot of shared UI stuff that has gotten gummed up depending on the use case, and it would be easier during testing (I have multiple devices) if I could just touch a button to restart the app completely.
So, does anyone have any suggestions on how to:
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 you close/stop/kill it.
Any tips would be appreciated! Thank you.
activity. startActivity(intent); // Start the launch activity System. exit(0); // System finishes and automatically relaunches us. }
You cannot restart an iOS Application in any case, even if you're able to do using some private api your application will be rejected by Apple and will not be considered for App store release.
Use the below code for restart the app:
Intent mStartActivity = new Intent(HomeActivity.this, SplashScreen.class);
int mPendingIntentId = 123456;
PendingIntent mPendingIntent = PendingIntent.getActivity(HomeActivity.this, mPendingIntentId, mStartActivity,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager) HomeActivity.this.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
System.exit(0);
As you can figure out, finish() is what you want to use in order to kill off an activity. A.C.R.'s way would work, however it will only restart your activity, not really kill off the process, and start it back up. If that's what you are looking for, instead of having a dummy Activity that restarts your original Activity, the correct way to do it would be to use flags.
Intent i = new Intent(this, WrapperActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
Those flags will clear your back stack all the way down to the first instance of whatever Activity you are creating, it will kill it, and then create a new one. This is essentially what A.C.R's example is doing, but it's much more concise.
If this isn't good enough for you, In order to do this properly, it's quite a bit of work, and requires more advanced knowledge of the Android system. You'd want to create a service that's running in the background (will need to be a separate process if you want the application level state killed) that you could startup when you wanted to kill the app, have the app kill itself, or have the service kill the app for you, and then have the service launch an intent that would start your activity/application back up.
Hope this helps! Best of luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With