https://github.com/lovoo/lastwords
lastwords is a little library written solely in Kotlin which notifies you when your app is terminated - that is all activities are either finishing or have been destroyed.
https://github.com/lovoo/lastwords
android android-library lifecycle lovoo
Last synced: about 2 months ago
JSON representation
lastwords is a little library written solely in Kotlin which notifies you when your app is terminated - that is all activities are either finishing or have been destroyed.
- Host: GitHub
- URL: https://github.com/lovoo/lastwords
- Owner: lovoo
- License: apache-2.0
- Created: 2017-03-10T17:49:41.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-05-31T12:33:52.000Z (about 8 years ago)
- Last Synced: 2025-01-16T09:07:26.097Z (over 1 year ago)
- Topics: android, android-library, lifecycle, lovoo
- Language: Kotlin
- Homepage:
- Size: 135 KB
- Stars: 1
- Watchers: 31
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://jitpack.io/#lovoo/lastwords)
# lastwords
_lastwords_ is a little library written purely in Kotlin which notifies you when your app is terminated - that is all activities are either finishing or have been destroyed.
## A short definition of a _finished_ app
For _lastwords_ a finished app is an app whose activities are all either _finishing_ or _destroyed_. So yes, we are excluding things like services here.
This proved to be a rather flexible definition for most use cases. If you stumble upon a use case which isn't covered by this feel free to create an issue and we'll discuss.
## Import
_lastwords_ is hosted on JitPack. Therefore you can simply import it by adding
```groovy
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
```
to your projects `build.gradle`.
Then add this to you app's `build.gradle`:
```groovy
dependencies {
compile 'com.github.lovoo:lastwords:1.1.0'
}
```
## Usage
Using _lastwords_ is very simple. In your Application class add the following initialisation code to the `onCreate()` method:
```kotlin
override fun onCreate() {
super.onCreate()
LastWords.init(this)
}
```
In case you don't have an own subclass of the Application class yet, kindly refer to the documentation of [Application](https://developer.android.com/reference/android/app/Application.html).
Whenever you want to be notified of the app finishing just register a listener:
```kotlin
LastWords.register(object : LastWords.Listener {
override fun onAppFinished() {
// do whatever you want here: tracking, cleaning up, herding llamas...
Toast.makeText(this@Application, "App finished", Toast.LENGTH_SHORT).show()
}
})
```
Of course you can register as many listeners as you want.
To stop listening use `LastWords.unregister(listener)` - you don't want to create nasty memory leaks, do you?
If you want to kill your app process with _lastwords_ you can use:
```kotlin
LastWords.finishApp(killDelay)
```
This will finish all Activities, then wait for the given delay and kill your process (if no other Activity is started in the meantime).
An example would be to notify the user that changes took place after App restart and you show him an option to close the app.
But do NOT call this if you have running tasks, like Database or Filesystem writing!!!
And that's it. Keep in mind that the system may kill the app process at any point in time (especially as it is effectively backgrounded now), so it's not _guaranteed_ that the code in the listener is executed - but what is guaranteed on our beloved Android anyway? ;)
However it will run happily in the overwhelming majority of cases which usually is better than not executing the code at all - that means it is definitely good for clean up tasks, tracking.