From 20f0d085d6940be30b076a8cff3de25fe4a6e21a Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Wed, 14 Feb 2018 20:18:18 -0500 Subject: Misc (#137) * Reorder final override * Update restart methods * Update changelog --- .../kotlin/ca/allanwang/kau/utils/ActivityUtils.kt | 33 ++++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'core/src/main/kotlin') diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt index fb88e61..833ec3a 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt @@ -2,11 +2,15 @@ package ca.allanwang.kau.utils import android.annotation.SuppressLint import android.app.Activity +import android.app.AlarmManager +import android.app.PendingIntent import android.content.Context import android.content.Intent import android.graphics.Color +import android.os.Build import android.os.Bundle import android.support.annotation.ColorInt +import android.support.annotation.RequiresApi import android.support.annotation.StringRes import android.support.design.widget.Snackbar import android.view.Menu @@ -56,7 +60,9 @@ inline fun Activity.startActivityForResult( */ inline fun Activity.restart(intentBuilder: Intent.() -> Unit = {}) { val i = Intent(this, this::class.java) - i.putExtras(intent.extras) + val oldExtras = intent.extras + if (oldExtras != null) + i.putExtras(oldExtras) i.intentBuilder() startActivity(i) overridePendingTransition(R.anim.kau_fade_in, R.anim.kau_fade_out) //No transitions @@ -64,6 +70,25 @@ inline fun Activity.restart(intentBuilder: Intent.() -> Unit = {}) { overridePendingTransition(R.anim.kau_fade_in, R.anim.kau_fade_out) } + +/** + * Force restart an entire application + */ +@Suppress("NOTHING_TO_INLINE") +@RequiresApi(Build.VERSION_CODES.M) +inline fun Activity.restartApplication() { + val intent = packageManager.getLaunchIntentForPackage(packageName) + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK) + val pending = PendingIntent.getActivity(this, 666, intent, PendingIntent.FLAG_CANCEL_CURRENT) + val alarm = getSystemService(Context.ALARM_SERVICE) as AlarmManager + if (buildIsMarshmallowAndUp) + alarm.setExactAndAllowWhileIdle(AlarmManager.RTC, System.currentTimeMillis() + 100, pending) + else + alarm.setExact(AlarmManager.RTC, System.currentTimeMillis() + 100, pending) + finish() + System.exit(0) +} + fun Activity.finishSlideOut() { finish() overridePendingTransition(R.anim.kau_fade_in, R.anim.kau_slide_out_right_top) @@ -113,8 +138,6 @@ fun Activity.hideKeyboard() = currentFocus.hideKeyboard() fun Activity.showKeyboard() = currentFocus.showKeyboard() -fun Activity.snackbar(text: String, duration: Int = Snackbar.LENGTH_LONG, builder: Snackbar.() -> Unit = {}) - = contentView!!.snackbar(text, duration, builder) +fun Activity.snackbar(text: String, duration: Int = Snackbar.LENGTH_LONG, builder: Snackbar.() -> Unit = {}) = contentView!!.snackbar(text, duration, builder) -fun Activity.snackbar(@StringRes textId: Int, duration: Int = Snackbar.LENGTH_LONG, builder: Snackbar.() -> Unit = {}) - = contentView!!.snackbar(textId, duration, builder) \ No newline at end of file +fun Activity.snackbar(@StringRes textId: Int, duration: Int = Snackbar.LENGTH_LONG, builder: Snackbar.() -> Unit = {}) = contentView!!.snackbar(textId, duration, builder) \ No newline at end of file -- cgit v1.2.3