How do I restart an Android Activity
? I tried the following, but the Activity
simply quits.
public static void restartActivity(Activity act){
Intent intent=new Intent();
intent.setClass(act, act.getClass());
act.startActivity(intent);
act.finish();
}
you can simply use
method as follows
and call onRestart() where you want to restart the current Activity
I used this code so I still could support older Android versions and use
recreate()
on newer Android versions.Code:
Sample:
try to use this ..
Just to combine Ralf and Ben's answers (including changes made in comments):
Actually the following code is valid for API levels 5 and up, so if your target API is lower than this, you'll end up with something very similar to EboMike's code.
There is one hacky way that should work on any activity, including the main one.
When orientation changes, Android generally will recreate your activity (unless you override it). This method is useful for 180 degree rotations, when Android doesn't recreate your activity.