How do I programmatically “restart” an Android app?

后端 未结 26 2456
小鲜肉
小鲜肉 2020-11-22 12:42

Firstly, I know that one should not really kill/restart an application on Android. In my use case, I want to factory-reset my application in a specific case where a server s

26条回答
  •  梦谈多话
    2020-11-22 13:07

    IntentCompat.makeMainSelectorActivity - Last tested on 11/2020

    The app will be restored with the launcher activity and the old process will be killed.

    Works from Api 15.

    public static void restart(Context context){
        Intent mainIntent = IntentCompat.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_LAUNCHER);
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.getApplicationContext().startActivity(mainIntent);
        System.exit(0);
    }
    

提交回复
热议问题