Reload / Restart an Activity in Android

Restarting an activity is pretty straight forward if your app supports HoneyComb and above. Just use Activiy.recreate. However, when supporting lower api versions you need something equivalent to Activity.recreate. I found this method to be useful. If you need to restart an activity from more than one activty it can be put in a utility class.

/**
Current Activity instance will go through its lifecycle to onDestroy() and a new instance then created after it.
*/
@SuppressLint("NewApi")
  public static final void recreateActivityCompat(final Activity a) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
      a.recreate();
    } else {
      final Intent intent = a.getIntent();
      intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
      a.finish();
      a.overridePendingTransition(0, 0);
      a.startActivity(intent);
      a.overridePendingTransition(0, 0);
   }
}

3 Responses

  1. Srikanth says:

    Thanks a lot……

  2. Tajiri says:

    This is awesome, and it works well.

  3. Morpheo says:

    this works… thank you so much!