Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added backpress confirmation on app exit #82

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.doubtless.doubtless.screens.main

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.appcompat.app.AppCompatActivity
import com.doubtless.doubtless.R
import com.doubtless.doubtless.databinding.ActivityMainBinding
import com.doubtless.doubtless.navigation.BackPressDispatcher
import com.doubtless.doubtless.navigation.OnBackPressListener
import com.google.android.material.snackbar.Snackbar


class MainActivity : AppCompatActivity(), BackPressDispatcher {

private var doubleBackToExitPressedOnce: Boolean = false
private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -52,8 +57,26 @@ class MainActivity : AppCompatActivity(), BackPressDispatcher {
}
}

if (!backPressConsumed)
super.onBackPressed()
if (!backPressConsumed) {
when {
doubleBackToExitPressedOnce -> super.onBackPressed()
else -> {
this.doubleBackToExitPressedOnce = true
Snackbar.make(
binding.container,
R.string.press_again_to_exit,
Snackbar.LENGTH_SHORT
).show()
Handler(Looper.getMainLooper()).postDelayed({
doubleBackToExitPressedOnce = false
}, BACKPRESS_DELAY)
}
}
}
}

companion object {
private const val BACKPRESS_DELAY = 2000L
}

}
18 changes: 18 additions & 0 deletions app/src/main/java/com/doubtless/doubtless/utils/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import android.animation.AnimatorInflater
import android.content.Context
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.annotation.AnimatorRes
import androidx.annotation.StringRes

fun View.addStateListAnimation(@AnimatorRes animation: Int) {
this.stateListAnimator = AnimatorInflater.loadStateListAnimator(
Expand All @@ -16,4 +18,20 @@ fun View.addStateListAnimation(@AnimatorRes animation: Int) {
fun View.hideSoftKeyboard() {
val imm = this.context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
imm?.hideSoftInputFromWindow(this.windowToken, 0)
}

fun Context.shortToast(@StringRes msg: Int) {
Toast.makeText(
this,
msg,
Toast.LENGTH_SHORT
).show()
}

fun Context.longToast(@StringRes msg: Int) {
Toast.makeText(
this,
msg,
Toast.LENGTH_LONG
).show()
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
<string name="text_choice_tag_3">Placements</string>
<string name="select_at_max_tags">Please select at max 3 tags</string>
<string name="sign_in_again">Please sign in again!</string>
<string name="press_again_to_exit">Press again to exit</string>
</resources>