Restarting Android app programmatically

前端 未结 9 2009
独厮守ぢ
独厮守ぢ 2021-02-03 12:14

This is a follow up question to this question:

Force application to restart on first activity

I am trying to restart my application from a fragment like that:

9条回答
  •  梦如初夏
    2021-02-03 12:49

    As the accepted answer doesn't seem to work on Android Q anymore due to the new background launch limitations, let me add my own solution:

    startActivity(
        Intent(applicationContext, MainActivity::class.java)
    )
    System.exit(0)
    

    Although it looks strange, it works very reliably for me, as the intent takes longer to fire than the system exit does. Plus, it is fired from the main thread, so Android Q won't complain.

提交回复
热议问题