Hey guys. I’ve just published my first Android application to show this guy who might hire me for his research, and I’ve got some good reviews except for one problem – my activity force closes with my current code to restart! I dont have an android phone so I cant test it out. Is there a simple way to just restart an app from the beginning? Apparently it force closes with a null pointer exception with this code:
Intent intent = getIntent();
finish();
startActivity(intent);
which I got from another question.
Please help!! I need to have this fixed
Thanks so much
As also suggested by the previous answer, that’s really not how the process lifecycle works. What you can do is take all of your initialization code out of your Activity’s onCreate method (including setContentView) and move it to a separate method, e.g. loadUi(). Then when you need to refresh the state, call loadUi() again and it will appear as though you’ve restarted, it will just be much faster.
If you were really stuck on restarting, you could do it by starting a service and then calling finish(), and having some code in the service launch your activity. You probably don’t want to do that, but if you are still determined then take a look at http://developer.android.com/reference/android/app/Service.html and http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29