Mariusz
Mariusz

Reputation: 1410

Restart singleTask Activity

i write launcher activity that have option to show or hide wallpaper to do that i use two theme

android:Theme.Holo.Light.NoActionBar

and

android:Theme.Wallpaper.NoTitleBar

to change it i do

@Override
protected void onNewIntent(final Intent intent) {
    super.onNewIntent(intent);
    Utils.PrintInfo("MainActivity.onNewIntent");


    if (AppSettings.Data.WallpaperThemeChanged) {
        AppSettings.Data.WallpaperThemeChanged = false;


        startActivity(new Intent(this, ThemeReloadActivity.class));

        finish();
        return;
    }
}

with ThemeReloadActivity like this

public class ThemeReloadActivity extends Activity {

@Override
protected void onResume() {

    Utils.PrintError("ThemeReloadActivity.onCreate");

    Activity activity = MainActivity.getMainActivity();
    activity.finish();
    startActivity(new Intent(activity, activity.getClass()));
    super.onResume();
}
}

and this is my Manifest fragment for that activity

<activity
        android:name="com.maxcom.launcher.MainActivity"
        android:clearTaskOnLaunch="true"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:screenOrientation="portrait"
        android:stateNotNeeded="true" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

but sometimes if i spam home theme does not change, it looks like app does not restart at all

Upvotes: 0

Views: 3994

Answers (3)

kundan bora
kundan bora

Reputation: 797

If you want to only restart activity you can do this

this.recreate();

this will recreate your activity.

Upvotes: 0

farid_z
farid_z

Reputation: 1673

        // get rid of this activity instance and start a new one
        finish();
        Intent intent = new Intent(MyApp.getInstance(), MyActivity.class);
        startActivity(intent);

Upvotes: 0

Chandrakanth
Chandrakanth

Reputation: 3831

You can restart your application by the following code

Intent intent = getBaseContext().getPackageManager()
            .getLaunchIntentForPackage(getBaseContext().getPackageName());
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);

Upvotes: 7

Related Questions