howto programatically "restart" android app?
<p>Firstly, I know that one should not really kill/restart an Application in Android. In my use case I want to factory-reset my application in a specific case where a server sends a specific information to the client. The user can only be logged in on the server with ONE instance of the application (i.e. multiple devices are not allowed). If another instance gets that "logged-in"-lock all other instanecs of that user have to delete their data (factory-reset), because of consistency. It is possible to force to get the lock, because the user might delete the app and reinstall it what would result in a different instance-id and the user could not free the lock anymore. Therefore it is possible to force to get the lock.</p>

<p>Because of that force-possibility we need to always check in an concrete instances that it has the lock. That is done on (almost) each request to the server. The server might send a "wrong-lock-id". If that is detected, the client application must delete everything.</p>

<hr>

<p>That was the use-case. No for the implementation-question:</p>

<p>I have an <code>Activity</code> A that starts the Login <code>Activity</code> L or the apps main <code>Activity</code> B depending on a sharedPrefs field. After starting L or B it closes itself so that only L or B is running.
So in the case that the user is logged in already B is running now.</p>

<p>B starts C. C calls <code>startService</code> for the <code>IntentService</code> D. That results in this stack:</p>

<p>(A) > B > C > D</p>

<p>from the onHandleIntent method of D an event is send to a <a href="http://file:///D:/android-sdk-windows/docs/reference/android/os/ResultReceiver.html">ResultReceiver</a> R.</p>

<p>R now handles that event by providing the user a dialog where he can choose to factory-reset the application (delete db, sharedPrefs etc.)</p>

<p>After the factory-reset I want to restart the application (to close all activities) and only start A again which then launches the Login <code>Activity</code> L and finishes itself:</p>

<p>(A) > L</p>

<p>The Dialogs onClick-method looks like this:</p>

<pre><code>@Override
public void onClick(DialogInterface dialog, int which) {
    // will call onCancelListener
    MyApplication.factoryReset(); // (deletes db, clears sharedPrefs etc.)
    Intent i = new Intent(MyApp.getContext(), A.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    MyApp.getContext().startActivity(i);
}
</code></pre>

<p>And thats the <code>MyApp</code> class:</p>

<pre><code>public class MyApp extends Application {
    private static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
    }

    public static Context getContext() {
        return context;
    }

    public static void factoryReset() {
        // ...
    }
}
</code></pre>

<p>The Problem now is that if I use the <code>FLAG_ACTIVITY_NEW_TASK</code> the Activities B and C are still running. If I hit the back btn on the Login <code>Activity</code> I see C, but I want to go back to the Home screen.
If I do not set the <code>FLAG_ACTIVITY_NEW_TASK</code> I get the error:</p>

<pre><code>07-07 12:27:12.272: ERROR/AndroidRuntime(9512): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
</code></pre>

<p>I cannot use the Activities' <code>Context</code>, because the <code>ServiceIntent</code> D might also be called from an background task which is started by the <code>AlarmManager</code>.</p>

<p>So how could I solve this to the activity stack becoming: (A) > L ?</p>
