How to restart Activity in Android

后端 未结 21 2273
日久生厌
日久生厌 2020-11-22 08:14

How do I restart an Android Activity? I tried the following, but the Activity simply quits.

public static void restartActivity(Act         


        
21条回答
  •  长发绾君心
    2020-11-22 08:28

    I did my theme switcher like this:

    Intent intent = getIntent();
    finish();
    startActivity(intent);
    

    Basically, I'm calling finish() first, and I'm using the exact same intent this activity was started with. That seems to do the trick?

    UPDATE: As pointed out by Ralf below, Activity.recreate() is the way to go in API 11 and beyond. This is preferable if you're in an API11+ environment. You can still check the current version and call the code snippet above if you're in API 10 or below. (Please don't forget to upvote Ralf's answer!)

提交回复
热议问题