Restarting Android app programmatically

前端 未结 9 2002
独厮守ぢ
独厮守ぢ 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:34

    Add this code as per your activity and your app will restart. This code is in kotlin.

    val intent = Intent(baseContext, MainActivity::class.java)
    val pendingIntentId = 101
    val pendingIntent = PendingIntent.getActivity(this, pendingIntentId,intent, PendingIntent.FLAG_CANCEL_CURRENT)
    val alarmManager = (this.getSystemService(Context.ALARM_SERVICE)) as AlarmManager
    alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 100, pendingIntent)
    exitProcess(0)
    

提交回复
热议问题