Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restart android application from within application

I have a requirement to restart the application when the user changes a preference. Clearing the stack doesnt help me since this doesnt cancel the backend service calls. I want to kill the application process itself. I am using

Process.killProcess(Process.myPid());

and it works for me to kill the application. But what i need is to restart the application. Means kill the process and trigger a new process so the application start fresh once again.

Is there a way to do this?

Thanks in advance.

like image 829
Akh Avatar asked Dec 02 '10 20:12

Akh


People also ask

How do you auto restart an Android application after a crash or a force close error?

See below how to do these: Call Thread. setDefaultUncaughtExceptionHandler() in order to catch all uncaught exception, in which case uncaughtException() method will be called. "Force close" will not appear and the application will be unresponsive, which is not a quite good thing.


2 Answers

This is not something which one should probably attempt to do outside of a testing environment.

That said, two ideas:

1) Set an alarm for some time in the very near future and then kill your process

2) Start up something else (perhaps a small native process or shell script) that will detect your death and restart you via an intent

You could also try firing off an intent to start yourself and then dying quickly, but this sounds like a potential race condition depending on implementation. If you grabbed the binder fd out of /proc and did evil things in native code, you might be able to fire off the intent in such a way that your application crashes on the return from the ioctl...

like image 55
Chris Stratton Avatar answered Sep 24 '22 22:09

Chris Stratton


Android is not designed to do this, and this is not a "requirement". This is an implementation. What exactly is the requirement? Why can't you design your app to handle preference changes without a restart? That seems like a very poor solution.

like image 26
Falmarri Avatar answered Sep 22 '22 22:09

Falmarri