Skip to content

Commit

Permalink
feat: 홈화면 뒤로가기 두 번 클릭 시 종료
Browse files Browse the repository at this point in the history
  • Loading branch information
SeongHoonC committed Oct 23, 2023
1 parent 192fb8c commit 974cd8f
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.addCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
Expand Down Expand Up @@ -37,6 +38,7 @@ class HomeActivity : AppCompatActivity() {
initView()
initObserve()
initResultLauncher()
initBackPressedDispatcher()
}

private fun initResultLauncher() {
Expand Down Expand Up @@ -129,6 +131,22 @@ class HomeActivity : AppCompatActivity() {
resultLauncher.launch(SignInActivity.getIntent(this))
}

private fun initBackPressedDispatcher() {
var backPressedTime = START_BACK_PRESSED_TIME
onBackPressedDispatcher.addCallback {
if ((System.currentTimeMillis() - backPressedTime) > FINISH_BACK_PRESSED_TIME) {
backPressedTime = System.currentTimeMillis()
Toast.makeText(
this@HomeActivity,
getString(R.string.home_back_pressed),
Toast.LENGTH_SHORT,
).show()
} else {
finish()
}
}
}

private inline fun <reified T : Fragment> changeFragment() {
val tag = T::class.java.name
val fragmentTransaction = supportFragmentManager.beginTransaction()
Expand All @@ -150,6 +168,8 @@ class HomeActivity : AppCompatActivity() {
}

companion object {
private const val START_BACK_PRESSED_TIME = 0L
private const val FINISH_BACK_PRESSED_TIME = 3000L
fun getIntent(context: Context): Intent {
return Intent(context, HomeActivity::class.java)
}
Expand Down

0 comments on commit 974cd8f

Please sign in to comment.