Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically relaunch/recreate an activity?

Tags:

android

People also ask

How to restart an activity programmatically?

Define an intent starterIntent in your class and assign it in onCreate() using starterIntent = getIntent(); . Then when you want to restart the activity, call finish(); startActivity(starterIntent); It isn't a very elegant solution, but it's a simple way to restart your activity and force it to reload everything.

How to recreate the same activity in Android?

From the documentation for recreate() : Cause this Activity to be recreated with a new instance. This results in essentially the same flow as when the Activity is created due to a configuration change -- the current instance will go through its lifecycle to onDestroy() and a new instance then created after it.

When activity is recreated?

Problem 1: activity recreation In an Android app, activities get recreated all the time. In particular, an activity is recreated anytime a configuration change happens. A user rotating their device is a configuration change, but configuration changes are increasingly common on newer versions of Android.

How do I recreate fragments?

attach() for recreating the fragment. Re-attach a fragment after it had previously been deatched from the UI with detach(Fragment). This causes its view hierarchy to be re-created, attached to the UI, and displayed. Detach the given fragment from the UI.


UPDATE: Android SDK 11 added a recreate() method to activities.


I've done that by simply reusing the intent that started the activity. Define an intent starterIntent in your class and assign it in onCreate() using starterIntent = getIntent();. Then when you want to restart the activity, call finish(); startActivity(starterIntent);

It isn't a very elegant solution, but it's a simple way to restart your activity and force it to reload everything.


Call the recreate method of the activity.


Option 1

Call recreate() on your Activity. However this method causes a flashing black screen to appear during the activity re-creation.

Option 2

finish();
startActivity(getIntent());

No "flashing" black screen here, but you'll see a transition between the old and the new instances with a not-so-pleasant black background. We can do better.

Option 3

To fix this, we can add a call to overridePendingTransition() :

finish();
startActivity(getIntent());
overridePendingTransition(0, 0);

Good bye black screen, but in my case I still see some kind of transition (a fade animation), on a colored background this time. That's because you're finishing the current instance of your activity before the new one is created and becomes fully visible, and the in-between color is the value of the windowBackground theme attribute.

Option 4

startActivity(getIntent());
finish();

Calling finish() after startActivity() will use the default transition between activities, often with a little slide-in animation. But the transition is still visible.

Option 5

startActivity(getIntent());
finish();
overridePendingTransition(0, 0);

To me, this is the best solution because it restarts the activity without any visible transition, like if nothing happened.

It could be useful if, for example, in your app you expose a way to change the display language independently of the system's language. In this case, whenever the user changes your app's language you'll probably want to restart your activity without transition, making the language switch look instantaneous.


Combining some answers here you can use something like the following.

class BaseActivity extends SherlockFragmentActivity
{
    // Backwards compatible recreate().
    @Override
    public void recreate()
    {
        if (android.os.Build.VERSION.SDK_INT >= 11)
        {
            super.recreate();
        }
        else
        {
            startActivity(getIntent());
            finish();
        }
    }
}

Testing

I tested it a bit, and there are some problems:

  1. If the activity is the lowest one on the stack, calling startActivity(...); finish(); just exist the app and doesn't restart the activity.
  2. super.recreate() doesn't actually act the same way as totally recreating the activity. It is equivalent to rotating the device so if you have any Fragments with setRetainInstance(true) they won't be recreated; merely paused and resumed.

So currently I don't believe there is an acceptable solution.


When I need to restart an activity, I use following code. Though it is not recommended.

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