完成Preference Activity中的所有活动

时间:2014-08-21 14:42:52

标签: android android-activity android-preferences

我有一个带有自定义DialogFragment的PreferenceActivity用于清除应用程序数据,因此我希望当用户单击是,应用程序要么完全关闭(完成所有活动),要么告诉它转到InitialSetup活动,所以应用程序可以重新设置。到目前为止,我还没有能够以任何方式做到这一点...... 试过

            Intent intent = new Intent(context.getApplicationContext(), MySettings.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            context.startActivity(intent);

但仍然没有关闭后台堆栈中的所有活动......

我该怎么做?

2 个答案:

答案 0 :(得分:3)

您需要的FLAG_ACTIVITY_CLEAR_TASKFLAG_ACTIVITY_NEW_TASK标志符合您的意图:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

或者如上所述,Sartheris将IntentCompat用于11以下的Android API:

Intent intent = IntentCompat.makeRestartActivityTask(new ComponentName(context, MySettings.class));
startActivity(intent);

答案 1 :(得分:0)

如果您只想让应用关闭,用户必须稍后再次启动它,您可以使用

    System.exit(0);
像普通Java一样。

如果您希望自己的应用在关闭后自动重启,请使用PendingIntent作为此问题的答案:

how to programmatically "restart" android app?