Skip to content

Commit

Permalink
refactor: Replace deprecated onBackPressed()
Browse files Browse the repository at this point in the history
  • Loading branch information
kongwoojin committed Feb 13, 2025
1 parent 428820d commit b0fc8ba
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.webkit.*
import androidx.activity.OnBackPressedCallback
import `in`.koreatech.koin.core.R
import `in`.koreatech.koin.core.databinding.ActivityWebviewBinding
import `in`.koreatech.koin.core.toast.ToastUtil
Expand All @@ -16,6 +17,16 @@ class WebViewActivity : ActivityBase(R.layout.activity_webview) {
private val binding by dataBinding<ActivityWebviewBinding>()
override val screenTitle: String = "웹뷰"

override val onBackPressedCallback: OnBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (binding.webView.canGoBack()) {
binding.webView.goBack()
} else {
finish()
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val title = intent.getStringExtra("title") ?: ""
Expand All @@ -29,17 +40,9 @@ class WebViewActivity : ActivityBase(R.layout.activity_webview) {
return super.onCreateOptionsMenu(menu)
}

override fun onBackPressed() {
if (binding.webView.canGoBack()) {
binding.webView.goBack()
} else {
super.onBackPressed()
}
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.home -> onBackPressed()
R.id.home -> onBackPressedDispatcher.onBackPressed()
R.id.menu_webview_finish -> finish()
}
return super.onOptionsItemSelected(item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ChangePasswordActivity : ActivityBase() {
ChangePasswordPageAdapter(this)
}

val onBackPressedCallback: OnBackPressedCallback by lazy {
override val onBackPressedCallback: OnBackPressedCallback by lazy {
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (viewModel.currentStep.value == ChangePasswordPage.entries.first())
Expand All @@ -46,7 +46,6 @@ class ChangePasswordActivity : ActivityBase() {
setContentView(binding.root)

initView()
initListeners()
initObservers()
}

Expand All @@ -65,10 +64,6 @@ class ChangePasswordActivity : ActivityBase() {
}
}

private fun initListeners() {
onBackPressedDispatcher.addCallback(this, onBackPressedCallback)
}

private fun initObservers() {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
Expand Down
27 changes: 13 additions & 14 deletions koin/src/main/java/in/koreatech/koin/ui/dining/DiningActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ class DiningActivity : KoinNavigationDrawerActivity() {
}
}

override val onBackPressedCallback: OnBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (isTaskRoot) {
val intent = Intent(this@DiningActivity, MainActivity::class.java)
startActivity(intent)
finish()
} else {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
}
}
}
@Inject
lateinit var onboardingManager: OnboardingManager

Expand Down Expand Up @@ -93,7 +105,7 @@ class DiningActivity : KoinNavigationDrawerActivity() {

binding.koinBaseAppBarDark.setOnClickListener {
when (it.id) {
AppBarBase.getLeftButtonId() -> onBackPressed()
AppBarBase.getLeftButtonId() -> onBackPressedDispatcher.onBackPressed()
AppBarBase.getRightButtonId() -> {
EventLogger.logClickEvent(
EventAction.CAMPUS,
Expand All @@ -104,19 +116,6 @@ class DiningActivity : KoinNavigationDrawerActivity() {
}
}
}

onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (isTaskRoot) {
val intent = Intent(this@DiningActivity, MainActivity::class.java)
startActivity(intent)
finish()
} else {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
}
}
})
}

private fun selectInitialPositions() {
Expand Down
15 changes: 9 additions & 6 deletions koin/src/main/java/in/koreatech/koin/ui/error/ErrorActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import androidx.activity.OnBackPressedCallback
import androidx.core.view.isVisible
import `in`.koreatech.koin.BuildConfig
import `in`.koreatech.koin.R
Expand All @@ -18,6 +19,14 @@ class ErrorActivity : ActivityBase() {
private val binding by dataBinding<ActivityErrorBinding>(R.layout.activity_error)
override val screenTitle: String = "에러"

override var onBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
onBackPressedDispatcher.onBackPressed()
val goToHomeIntent = Intent(this@ErrorActivity, SplashActivity::class.java)
startActivity(goToHomeIntent)
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
Expand Down Expand Up @@ -53,12 +62,6 @@ class ErrorActivity : ActivityBase() {
finish()
}

override fun onBackPressed() {
super.onBackPressed()
val goToHomeIntent = Intent(this, SplashActivity::class.java)
startActivity(goToHomeIntent)
}

private fun onClickKakaoTalk() {
goToKakaoTalkBcsdlabFriend()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.TextView
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog
Expand Down Expand Up @@ -149,6 +150,26 @@ abstract class KoinNavigationDrawerActivity : ActivityBase(),
}.create()
}

override val onBackPressedCallback: OnBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (drawerLayout.isDrawerOpened()) {
drawerLayout.closeDrawer()
} else {
if (menuState == MenuState.Main) {
if (System.currentTimeMillis() > pressTime + 2000) {
pressTime = System.currentTimeMillis()
ToastUtil.getInstance().makeShort(getString(R.string.press_again_to_exit))
} else {
finishAffinity()
}
} else {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
}
}
}
}

override fun onPostCreate(savedInstanceState: Bundle?) {
super.onPostCreate(savedInstanceState)

Expand Down Expand Up @@ -263,23 +284,6 @@ abstract class KoinNavigationDrawerActivity : ActivityBase(),
leftNavigationView.layoutParams.apply { width = windowWidth }
}

override fun onBackPressed() {
if (drawerLayout.isDrawerOpened()) {
drawerLayout.closeDrawer()
} else {
if (menuState == MenuState.Main) {
if (System.currentTimeMillis() > pressTime + 2000) {
pressTime = System.currentTimeMillis()
ToastUtil.getInstance().makeShort(getString(R.string.press_again_to_exit))
} else {
finishAffinity()
}
} else {
super.onBackPressed()
}
}
}

private fun initDrawerViewModel() = with(koinNavigationDrawerViewModel) {
observeLiveData(menuEvent) { menuState ->
when (menuState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class StoreActivity : KoinNavigationDrawerTimeActivity() {
else -> {
viewModel.refreshStores()
isEnabled = false
onBackPressed()
onBackPressedDispatcher.onBackPressed()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import android.Manifest
import android.annotation.SuppressLint
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Intent
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.widget.TextView
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog
Expand All @@ -31,6 +33,7 @@ import `in`.koreatech.koin.core.util.dataBinding
import `in`.koreatech.koin.databinding.StoreActivityDetailBinding
import `in`.koreatech.koin.ui.navigation.KoinNavigationDrawerActivity
import `in`.koreatech.koin.ui.navigation.state.MenuState
import `in`.koreatech.koin.ui.splash.SplashActivity
import `in`.koreatech.koin.ui.splash.state.TokenState
import `in`.koreatech.koin.ui.store.adapter.StoreDetailFlyerRecyclerAdapter
import `in`.koreatech.koin.ui.store.adapter.StoreDetailImageViewpagerAdapter
Expand Down Expand Up @@ -92,6 +95,18 @@ class StoreDetailActivity : KoinNavigationDrawerActivity() {
}
private val storeDetailViewpagerAdapter = StoreDetailViewpagerAdapter(this)

override val onBackPressedCallback: OnBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (flyerDialogFragment?.isVisible == true) {
flyerDialogFragment!!.dismiss()
flyerDialogFragment = null
return
}
isEnabled = false
onBackPressedDispatcher.onBackPressed()
}
}

@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -108,7 +123,7 @@ class StoreDetailActivity : KoinNavigationDrawerActivity() {
AppBarBase.getLeftButtonId() -> {
storeElapsedTime = System.currentTimeMillis() - storeCurrentTime
isSwipeGesture = false
onBackPressed()
onBackPressedDispatcher.onBackPressed()
}

AppBarBase.getRightButtonId() -> {
Expand Down Expand Up @@ -266,15 +281,6 @@ class StoreDetailActivity : KoinNavigationDrawerActivity() {
return super.onTouchEvent(event)
}

override fun onBackPressed() {
if (flyerDialogFragment?.isVisible == true) {
flyerDialogFragment!!.dismiss()
flyerDialogFragment = null
return
}
super.onBackPressed()
}

private fun initCallFunction() {
binding.storeDetailPhoneImage.setOnClickListener {
callingLogic()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class StoreReviewReportActivity: ActivityBase() {

reportAppbar.setOnClickListener {
when (it.id) {
AppBarBase.getLeftButtonId() -> onBackPressed()
AppBarBase.getLeftButtonId() -> onBackPressedDispatcher.onBackPressed()
}
}
storeName = intent.extras?.getString("storeName")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ class TimetableActivity : KoinNavigationDrawerActivity() {
private fun setAppbarEvent(rightButtonClickable: () -> Unit = {}) {
binding.koinBaseAppbar.setOnClickListener {
when (it.id) {
AppBarBase.getLeftButtonId() -> onBackPressed()
AppBarBase.getLeftButtonId() -> onBackPressedDispatcher.onBackPressed()
AppBarBase.getRightButtonId() -> rightButtonClickable()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class TimetableSemesterActivity : ActivityBase() {
private val binding by dataBinding<ActivityTimetableSemesterBinding>()
private val viewModel by viewModels<SemesterViewModel>()

private val onBackPressedCallback = object : OnBackPressedCallback(true) {
override val onBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (viewModel.screenState.value.userTimetableFrames.isEmpty()) {
finishActivityWithResult(
Expand Down Expand Up @@ -70,7 +70,6 @@ class TimetableSemesterActivity : ActivityBase() {
val frameName = bundle.getString(TimetableActivity.FRAME_NAME).orEmpty()
viewModel.updateIntentData(isAnonymous, frameId, semester, frameName)
}
onBackPressedDispatcher.addCallback(onBackPressedCallback)

binding.timetableListComposeView.setContent {
KoinTheme {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class UserInfoActivity : ActivityBase() {
private fun initView() = with(binding) {
appbarUserInfo.setAppBarButtonClickedListener(
leftButtonClicked = {
onBackPressed()
onBackPressedDispatcher.onBackPressed()
},
rightButtonClicked = {
userInfoEditActivityNew.launch(Unit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class UserInfoEditActivity : ActivityBase() {
private fun initView() = with(binding) {
appbarUserInfoEdit.setAppBarButtonClickedListener(
leftButtonClicked = {
onBackPressed()
onBackPressedDispatcher.onBackPressed()
},
rightButtonClicked = {}
)
Expand Down

0 comments on commit b0fc8ba

Please sign in to comment.