Skip to content

Commit

Permalink
[AN/USER] feat: 홈 화면에서 뒤로가기 누르면 곧바로 종료되지 않는다. (#588)
Browse files Browse the repository at this point in the history
* feat: 홈화면 뒤로가기 상수 추가

* feat: 홈화면 뒤로가기 두 번 클릭 시 종료
  • Loading branch information
SeongHoonC authored and seokjin8678 committed Nov 16, 2023
1 parent ff939d8 commit 9617972
Show file tree
Hide file tree
Showing 2 changed files with 21 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
1 change: 1 addition & 0 deletions android/festago/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<string name="home_bottom_navigation_ticket">티켓 목록</string>
<string name="home_bottom_navigation_user">마이페이지</string>
<string name="home_notification_permission_denied">알림 권한을 거부했습니다 :(</string>
<string name="home_back_pressed">한 번 더 누르면 앱이 종료됩니다</string>

<!-- Strings related to ticket reserve-->
<string name="ticket_reserve_tv_date_range">%1s ~ %1s</string>
Expand Down

0 comments on commit 9617972

Please sign in to comment.