Here are two short code snippets that I used to restart my Android application programmatically.
First, I added the following code in the onCreate method of the activity I would like to restart and saved references to the activity and the restart-intent to static attributes:
[...] @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); [...] ACTIVITY = this; RESTART_INTENT = PendingIntent.getActivity(this.getBaseContext(), 0, new Intent(getIntent()), getIntent().getFlags()); [...] } |
Second, I used the following code to restart the application:
AlarmManager mgr = (AlarmManager)ACTIVITY.getSystemService(Context.ALARM_SERVICE); mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, RESTART_INTENT); System.exit(2); |