diff options
author | Allan Wang <me@allanwang.ca> | 2018-02-14 20:18:18 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-14 20:18:18 -0500 |
commit | 20f0d085d6940be30b076a8cff3de25fe4a6e21a (patch) | |
tree | 417f2e67afdf53981344c4c3c4ba9f5636d8f8f1 | |
parent | e9111feb62bf245309d1ffde9f8579195d8f6449 (diff) | |
download | kau-20f0d085d6940be30b076a8cff3de25fe4a6e21a.tar.gz kau-20f0d085d6940be30b076a8cff3de25fe4a6e21a.tar.bz2 kau-20f0d085d6940be30b076a8cff3de25fe4a6e21a.zip |
Misc (#137)
* Reorder final override
* Update restart methods
* Update changelog
-rw-r--r-- | adapter/src/main/kotlin/ca/allanwang/kau/iitems/KauIItem.kt | 1 | ||||
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt | 33 | ||||
-rw-r--r-- | sample/src/main/res/xml/kau_changelog.xml | 3 |
3 files changed, 31 insertions, 6 deletions
diff --git a/adapter/src/main/kotlin/ca/allanwang/kau/iitems/KauIItem.kt b/adapter/src/main/kotlin/ca/allanwang/kau/iitems/KauIItem.kt index 7918a26..c01dea4 100644 --- a/adapter/src/main/kotlin/ca/allanwang/kau/iitems/KauIItem.kt +++ b/adapter/src/main/kotlin/ca/allanwang/kau/iitems/KauIItem.kt @@ -21,5 +21,4 @@ open class KauIItem<Item, VH : RecyclerView.ViewHolder>( final override fun getType(): Int = type final override fun getViewHolder(v: View): VH = viewHolder(v) final override fun getLayoutRes(): Int = layoutRes - }
\ No newline at end of file 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 <T : Activity> 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 diff --git a/sample/src/main/res/xml/kau_changelog.xml b/sample/src/main/res/xml/kau_changelog.xml index 31163d0..146042c 100644 --- a/sample/src/main/res/xml/kau_changelog.xml +++ b/sample/src/main/res/xml/kau_changelog.xml @@ -7,6 +7,9 @@ --> <version title="v3.6.4" /> + <item text=":core: Fix potential NPE in restart()" /> + <item text=":core: Create restartApplication()" /> + <item text="" /> <item text="Update documentation" /> <item text="" /> |