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

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

Restart a single task activity, If you're using API 11 and above you can use the: Activity.recreate(). If you need to support lower API use this to call your activity again: I was just trying to make Activity 'A' show itself again when the event occurs. It was only when I was failing to get the Intent to work that I started testing with a secondary ('B') Activity. - The actual app uses a third party API which triggers a callback event, which resides on the singleTask Activity 'A'.

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

Restarting a singleTask activity Solutions, I am having some trouble with bringing a singletask activity back into view. Manifest: <activity android:name=".test.MyActivity"  1. Activity Launch Mode SingleTask. When you request an instance of activity that configured with singleTask launch mode, if one instance has exist in current back stack, then that instance will be pop up to the top of the back stack and all other activity instance above it will be removed from the back stack. Below example has three activity.

If you want to only restart activity you can do this

this.recreate();

this will recreate your activity.

Finish activity before restarting it · Issue #1322 · signalapp/Signal , The current activity needs to be finished before calling startActivity. Otherwise, activities with launchMode singleTask (ConversationListActivity) will receive a  If the user later resumes the task by selecting the launcher icon that began the task, the task comes to the foreground and resumes the activity at the top of the stack. If the user presses the Back button, the current activity is popped from the stack and destroyed. The previous activity in the stack is resumed.

Looking for help with "singleTask" and resuming my app from , If there is no singleTask Activity instance in the system yet, a new one would If you are using the Single Task mode as is, it should not restart your entire app. The "singleTask" and "singleInstance" modes also differ from each other in only one respect: A "singleTask" activity allows other activities to be part of its task. It's always at the root of its task, but other activities (necessarily " standard " and " singleTop " activities) can be launched into that task.

Understand Tasks and Back Stack, "singleInstance" . Same as "singleTask" , except that the system doesn't launch any other activities into the task holding the instance. The activity  When most people start using Project 2013, they initially try to enter a start date for every task in the project. After all, you include dates when you create to-do lists, right? You’re jumping the gun, though, and missing out on one of the great strengths of project management software; the ability to start and …

<activity>, Since activities with " singleTask " or " singleInstance " launch modes about how to properly handle a restart due to a configuration change. As a user navigates through, out of, and back to your app, the Activity instances in your app transition through different states in their lifecycle. The Activity class provides a number of callbacks that allow the activity to know that a state has changed: that the system is creating, stopping, or resuming an activity, or destroying the process in which the activity resides.

Comments
  • Do you want to restart the total application or only one activity??
  • yea i try it but when i realize i can not restart application, i have to restart single activity would be best to do it in background but how can i achive this
  • To restart only one activity you can use "recreate()" method.
  • Thanks, It helped...(y)