1

I have a teclast M40 pro tablet running Android Version (V11) with the app luncher to only boot one app. Now there is a problem, that sometimes the Wifi does not reconnect and a app restart is necessary. To prevent a manual restart, I would like to kill the app from the app itself and let it boot again with the app luncher.

So, how can I kill the app (Software Restart) from the app itself?

HansPeterLoft
  • 489
  • 1
  • 9
  • 28
  • Does this answer your question? [How to quit android application programmatically](https://stackoverflow.com/questions/6330200/how-to-quit-android-application-programmatically) – JensV Feb 03 '22 at 17:50

2 Answers2

1

You can use System.exit(0) to kill it and PendingIntent to start your application. Considering SplashActivity is your starting point.

Intent intent = new Intent(context, MainActivity.class);
int intentId = 123;
PendingIntent pendingIntent = PendingIntent.getActivity(context, intentId,    intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
System.exit(0);
Taranmeet Singh
  • 1,199
  • 1
  • 11
  • 14
1

you can use:

fun triggerRestart(context: Activity) {
  val intent = Intent(context, MainActivity::class.java)
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
  context.startActivity(intent)
  if (context is Activity) {
     (context as Activity).finish()
  }
  Runtime.getRuntime().exit(0)
}

and for kotlin, replace

Runtime.getRuntime().exit(0)

with

kotlin.system.exitProcess(0)
amimic11
  • 363
  • 1
  • 6