以编程方式重启app?

Iva*_*ono 6 android

我需要以编程方式重新启动应用程序.我的启动器活动称为" 登录 ",登录后,主要活动称为" 主要 ".在主要活动中,我想重新启动应用程序.所以我有以下内容:

Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
Run Code Online (Sandbox Code Playgroud)

这将显示"登录"活动,但是当我按下时,我将返回上一个活动.

有没有更好的方法来真正重启应用程序?

joh*_*o07 21

试试下面的代码

Intent i = getBaseContext().getPackageManager().
           getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
Run Code Online (Sandbox Code Playgroud)

在调用第二个Activity之前还有一件事

finish();
Run Code Online (Sandbox Code Playgroud)