How to restart Activity in Android

AndroidAndroid Activity

Android Problem Overview


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();
   
}

Android Solutions


Solution 1 - Android

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!)

Solution 2 - Android

Since API level 11 (Honeycomb), you can call the recreate() method of the activity (thanks to this answer).

The recreate() method acts just like a configuration change, so your onSaveInstanceState() and onRestoreInstanceState() methods are also called, if applicable.

Solution 3 - Android

Before SDK 11, a way to do this is like so:

public void reload() {
	Intent intent = getIntent();
	overridePendingTransition(0, 0);
	intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
	finish();
	overridePendingTransition(0, 0);
	startActivity(intent);
}

Solution 4 - Android

Just to combine Ralf and Ben's answers (including changes made in comments):

if (Build.VERSION.SDK_INT >= 11) {
    recreate();
} else {
    Intent intent = getIntent();
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    finish();
    overridePendingTransition(0, 0);

    startActivity(intent);
    overridePendingTransition(0, 0);
}

Solution 5 - Android

I used this code so I still could support older Android versions and use recreate() on newer Android versions.

Code:

public static void restartActivity(Activity activity){
    if (Build.VERSION.SDK_INT >= 11) {
        activity.recreate();
    } else {
        activity.finish();
        activity.startActivity(activity.getIntent());
    }
}

Sample:

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Activity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mActivity = MainActivity.this;

        Button button = (Button) findViewById(R.id.restart_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                restartActivity(mActivity);
            }
        });
    }

    public static void restartActivity(Activity activity) {
        if (Build.VERSION.SDK_INT >= 11) {
            activity.recreate();
        } else {
            activity.finish();
            activity.startActivity(activity.getIntent());
        }
    }
}

Solution 6 - Android

This is by far the easiest way to restart the current activity:

finish();
startActivity(getIntent());

Solution 7 - Android

This solution worked for me.

First finish the activity and then start it again.

Sample code:

public void restartActivity(){
    Intent mIntent = getIntent();
    finish();
    startActivity(mIntent);
}

Solution 8 - Android

Call this method

private void restartFirstActivity()
 {
 Intent i = getApplicationContext().getPackageManager()
 .getLaunchIntentForPackage(getApplicationContext().getPackageName() );

 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
 startActivity(i);
 }

Thanks,

Solution 9 - Android

I wonder why no one mentioned Intent.makeRestartActivityTask() which cleanly makes this exact purpose.

> Make an Intent that can be used to re-launch an application's task * in its base state.

startActivity(Intent.makeRestartActivityTask(getActivity().getIntent().getComponent()));

This method sets Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK as default flags.

Solution 10 - Android

Even though this has been answered multiple times.

If restarting an activity from a fragment, I would do it like so:

new Handler().post(new Runnable() {

         @Override
         public void run()
         {
            Intent intent = getActivity().getIntent();
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
            getActivity().overridePendingTransition(0, 0);
            getActivity().finish();

            getActivity().overridePendingTransition(0, 0);
            startActivity(intent);
        }
    });

So you might be thinking this is a little overkill? But the Handler posting allows you to call this in a lifecycle method. I've used this in onRestart/onResume methods when checking if the state has changed between the user coming back to the app. (installed something).

Without the Handler if you call it in an odd place it will just kill the activity and not restart it.

Feel free to ask any questions.

Cheers, Chris

Solution 11 - Android

Well this is not listed but a combo of some that is already posted:

if (Build.VERSION.SDK_INT >= 11) {
    recreate();   
} else {
    Intent intent = getIntent();
    finish();
    startActivity(intent);
}

Solution 12 - Android

If anybody is looking for Kotlin answer you just need this line.

Fragment

startActivity(Intent.makeRestartActivityTask(activity?.intent?.component))

Activity

startActivity(Intent.makeRestartActivityTask(this.intent?.component))

Solution 13 - Android

There is one hacky way that should work on any activity, including the main one.

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

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.

Solution 14 - Android

In conjunction with strange SurfaceView lifecycle behaviour with the Camera. I have found that recreate() does not behave well with the lifecycle of SurfaceViews. surfaceDestroyed isn't ever called during the recreation cycle. It is called after onResume (strange), at which point my SurfaceView is destroyed.

The original way of recreating an activity works fine.

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

I can't figure out exactly why this is, but it is just an observation that can hopefully guide others in the future because it fixed my problems i was having with SurfaceViews

Solution 15 - Android

The solution for your question is:

public static void restartActivity(Activity act){
    Intent intent=new Intent();
    intent.setClass(act, act.getClass());
    ((Activity)act).startActivity(intent);
    ((Activity)act).finish();
}

You need to cast to activity context to start new activity and as well as to finish the current activity.

Hope this helpful..and works for me.

Solution 16 - Android

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.

intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
overridePendingTransition(0, 0);

Solution 17 - Android

public void onRestart() {
    super.onRestart();
    Intent intent=new Intent();
    intent.setClass(act, act.getClass());
    finish();
    act.startActivity(intent);
}

try to use this ..

Solution 18 - Android

If you remove the last line, you'll create new act Activity, but your old instance will still be alive.

Do you need to restart the Activity like when the orientation is changed (i.e. your state is saved and passed to onCreate(Bundle))?

If you don't, one possible workaround would be to use one extra, dummy Activity, which would be started from the first Activity, and which job is to start new instance of it. Or just delay the call to act.finish(), after the new one is started.

If you need to save most of the state, you are getting in pretty deep waters, because it's non-trivial to pass all the properties of your state, especially without leaking your old Context/Activity, by passing it to the new instance.

Please, specify what are you trying to do.

Solution 19 - Android

If you are calling from some fragment so do below code.

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

Solution 20 - Android

This is the way I do it.

        val i = Intent(context!!, MainActivity::class.java)
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
        startActivity(i)

Solution 21 - Android

Call the method onCreate. For example onCreate(null);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser157195View Question on Stackoverflow
Solution 1 - AndroidEboMikeView Answer on Stackoverflow
Solution 2 - AndroidRalfView Answer on Stackoverflow
Solution 3 - AndroidBenView Answer on Stackoverflow
Solution 4 - AndroidJustinMorrisView Answer on Stackoverflow
Solution 5 - AndroidThomas VosView Answer on Stackoverflow
Solution 6 - AndroidcodeView Answer on Stackoverflow
Solution 7 - Androiduser3748515View Answer on Stackoverflow
Solution 8 - AndroidNikhil DineshView Answer on Stackoverflow
Solution 9 - AndroidshkschneiderView Answer on Stackoverflow
Solution 10 - AndroidChris.JenkinsView Answer on Stackoverflow
Solution 11 - AndroidCodeversedView Answer on Stackoverflow
Solution 12 - AndroidOhhhThatVarunView Answer on Stackoverflow
Solution 13 - AndroidAchal DaveView Answer on Stackoverflow
Solution 14 - AndroidThe4thIcemanView Answer on Stackoverflow
Solution 15 - AndroidRajesh PeramView Answer on Stackoverflow
Solution 16 - AndroidSandyView Answer on Stackoverflow
Solution 17 - AndroidAmsheerView Answer on Stackoverflow
Solution 18 - AndroidDimitar DimitrovView Answer on Stackoverflow
Solution 19 - AndroidMihir TrivediView Answer on Stackoverflow
Solution 20 - AndroidMicroRJView Answer on Stackoverflow
Solution 21 - Androidkike0kikeView Answer on Stackoverflow